xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/clk/gt215.c (revision bbde9fc1824aab58bc78c084163007dd6c03fe5b)
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  *          Roy Spliet
24  */
25 #include "gt215.h"
26 #include "pll.h"
27 
28 #include <core/device.h>
29 #include <engine/fifo.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/pll.h>
32 #include <subdev/timer.h>
33 
34 struct gt215_clk_priv {
35 	struct nvkm_clk base;
36 	struct gt215_clk_info eng[nv_clk_src_max];
37 };
38 
39 static u32 read_clk(struct gt215_clk_priv *, int, bool);
40 static u32 read_pll(struct gt215_clk_priv *, int, u32);
41 
42 static u32
43 read_vco(struct gt215_clk_priv *priv, int clk)
44 {
45 	u32 sctl = nv_rd32(priv, 0x4120 + (clk * 4));
46 
47 	switch (sctl & 0x00000030) {
48 	case 0x00000000:
49 		return nv_device(priv)->crystal;
50 	case 0x00000020:
51 		return read_pll(priv, 0x41, 0x00e820);
52 	case 0x00000030:
53 		return read_pll(priv, 0x42, 0x00e8a0);
54 	default:
55 		return 0;
56 	}
57 }
58 
59 static u32
60 read_clk(struct gt215_clk_priv *priv, int clk, bool ignore_en)
61 {
62 	u32 sctl, sdiv, sclk;
63 
64 	/* refclk for the 0xe8xx plls is a fixed frequency */
65 	if (clk >= 0x40) {
66 		if (nv_device(priv)->chipset == 0xaf) {
67 			/* no joke.. seriously.. sigh.. */
68 			return nv_rd32(priv, 0x00471c) * 1000;
69 		}
70 
71 		return nv_device(priv)->crystal;
72 	}
73 
74 	sctl = nv_rd32(priv, 0x4120 + (clk * 4));
75 	if (!ignore_en && !(sctl & 0x00000100))
76 		return 0;
77 
78 	/* out_alt */
79 	if (sctl & 0x00000400)
80 		return 108000;
81 
82 	/* vco_out */
83 	switch (sctl & 0x00003000) {
84 	case 0x00000000:
85 		if (!(sctl & 0x00000200))
86 			return nv_device(priv)->crystal;
87 		return 0;
88 	case 0x00002000:
89 		if (sctl & 0x00000040)
90 			return 108000;
91 		return 100000;
92 	case 0x00003000:
93 		/* vco_enable */
94 		if (!(sctl & 0x00000001))
95 			return 0;
96 
97 		sclk = read_vco(priv, clk);
98 		sdiv = ((sctl & 0x003f0000) >> 16) + 2;
99 		return (sclk * 2) / sdiv;
100 	default:
101 		return 0;
102 	}
103 }
104 
105 static u32
106 read_pll(struct gt215_clk_priv *priv, int clk, u32 pll)
107 {
108 	u32 ctrl = nv_rd32(priv, pll + 0);
109 	u32 sclk = 0, P = 1, N = 1, M = 1;
110 
111 	if (!(ctrl & 0x00000008)) {
112 		if (ctrl & 0x00000001) {
113 			u32 coef = nv_rd32(priv, pll + 4);
114 			M = (coef & 0x000000ff) >> 0;
115 			N = (coef & 0x0000ff00) >> 8;
116 			P = (coef & 0x003f0000) >> 16;
117 
118 			/* no post-divider on these..
119 			 * XXX: it looks more like two post-"dividers" that
120 			 * cross each other out in the default RPLL config */
121 			if ((pll & 0x00ff00) == 0x00e800)
122 				P = 1;
123 
124 			sclk = read_clk(priv, 0x00 + clk, false);
125 		}
126 	} else {
127 		sclk = read_clk(priv, 0x10 + clk, false);
128 	}
129 
130 	if (M * P)
131 		return sclk * N / (M * P);
132 
133 	return 0;
134 }
135 
136 static int
137 gt215_clk_read(struct nvkm_clk *clk, enum nv_clk_src src)
138 {
139 	struct gt215_clk_priv *priv = (void *)clk;
140 	u32 hsrc;
141 
142 	switch (src) {
143 	case nv_clk_src_crystal:
144 		return nv_device(priv)->crystal;
145 	case nv_clk_src_core:
146 	case nv_clk_src_core_intm:
147 		return read_pll(priv, 0x00, 0x4200);
148 	case nv_clk_src_shader:
149 		return read_pll(priv, 0x01, 0x4220);
150 	case nv_clk_src_mem:
151 		return read_pll(priv, 0x02, 0x4000);
152 	case nv_clk_src_disp:
153 		return read_clk(priv, 0x20, false);
154 	case nv_clk_src_vdec:
155 		return read_clk(priv, 0x21, false);
156 	case nv_clk_src_daemon:
157 		return read_clk(priv, 0x25, false);
158 	case nv_clk_src_host:
159 		hsrc = (nv_rd32(priv, 0xc040) & 0x30000000) >> 28;
160 		switch (hsrc) {
161 		case 0:
162 			return read_clk(priv, 0x1d, false);
163 		case 2:
164 		case 3:
165 			return 277000;
166 		default:
167 			nv_error(clk, "unknown HOST clock source %d\n", hsrc);
168 			return -EINVAL;
169 		}
170 	default:
171 		nv_error(clk, "invalid clock source %d\n", src);
172 		return -EINVAL;
173 	}
174 
175 	return 0;
176 }
177 
178 int
179 gt215_clk_info(struct nvkm_clk *clock, int clk, u32 khz,
180 	       struct gt215_clk_info *info)
181 {
182 	struct gt215_clk_priv *priv = (void *)clock;
183 	u32 oclk, sclk, sdiv;
184 	s32 diff;
185 
186 	info->clk = 0;
187 
188 	switch (khz) {
189 	case 27000:
190 		info->clk = 0x00000100;
191 		return khz;
192 	case 100000:
193 		info->clk = 0x00002100;
194 		return khz;
195 	case 108000:
196 		info->clk = 0x00002140;
197 		return khz;
198 	default:
199 		sclk = read_vco(priv, clk);
200 		sdiv = min((sclk * 2) / khz, (u32)65);
201 		oclk = (sclk * 2) / sdiv;
202 		diff = ((khz + 3000) - oclk);
203 
204 		/* When imprecise, play it safe and aim for a clock lower than
205 		 * desired rather than higher */
206 		if (diff < 0) {
207 			sdiv++;
208 			oclk = (sclk * 2) / sdiv;
209 		}
210 
211 		/* divider can go as low as 2, limited here because NVIDIA
212 		 * and the VBIOS on my NVA8 seem to prefer using the PLL
213 		 * for 810MHz - is there a good reason?
214 		 * XXX: PLLs with refclk 810MHz?  */
215 		if (sdiv > 4) {
216 			info->clk = (((sdiv - 2) << 16) | 0x00003100);
217 			return oclk;
218 		}
219 
220 		break;
221 	}
222 
223 	return -ERANGE;
224 }
225 
226 int
227 gt215_pll_info(struct nvkm_clk *clock, int clk, u32 pll, u32 khz,
228 	       struct gt215_clk_info *info)
229 {
230 	struct nvkm_bios *bios = nvkm_bios(clock);
231 	struct gt215_clk_priv *priv = (void *)clock;
232 	struct nvbios_pll limits;
233 	int P, N, M, diff;
234 	int ret;
235 
236 	info->pll = 0;
237 
238 	/* If we can get a within [-2, 3) MHz of a divider, we'll disable the
239 	 * PLL and use the divider instead. */
240 	ret = gt215_clk_info(clock, clk, khz, info);
241 	diff = khz - ret;
242 	if (!pll || (diff >= -2000 && diff < 3000)) {
243 		goto out;
244 	}
245 
246 	/* Try with PLL */
247 	ret = nvbios_pll_parse(bios, pll, &limits);
248 	if (ret)
249 		return ret;
250 
251 	ret = gt215_clk_info(clock, clk - 0x10, limits.refclk, info);
252 	if (ret != limits.refclk)
253 		return -EINVAL;
254 
255 	ret = gt215_pll_calc(nv_subdev(priv), &limits, khz, &N, NULL, &M, &P);
256 	if (ret >= 0) {
257 		info->pll = (P << 16) | (N << 8) | M;
258 	}
259 
260 out:
261 	info->fb_delay = max(((khz + 7566) / 15133), (u32) 18);
262 	return ret ? ret : -ERANGE;
263 }
264 
265 static int
266 calc_clk(struct gt215_clk_priv *priv, struct nvkm_cstate *cstate,
267 	 int clk, u32 pll, int idx)
268 {
269 	int ret = gt215_pll_info(&priv->base, clk, pll, cstate->domain[idx],
270 				 &priv->eng[idx]);
271 	if (ret >= 0)
272 		return 0;
273 	return ret;
274 }
275 
276 static int
277 calc_host(struct gt215_clk_priv *priv, struct nvkm_cstate *cstate)
278 {
279 	int ret = 0;
280 	u32 kHz = cstate->domain[nv_clk_src_host];
281 	struct gt215_clk_info *info = &priv->eng[nv_clk_src_host];
282 
283 	if (kHz == 277000) {
284 		info->clk = 0;
285 		info->host_out = NVA3_HOST_277;
286 		return 0;
287 	}
288 
289 	info->host_out = NVA3_HOST_CLK;
290 
291 	ret = gt215_clk_info(&priv->base, 0x1d, kHz, info);
292 	if (ret >= 0)
293 		return 0;
294 
295 	return ret;
296 }
297 
298 int
299 gt215_clk_pre(struct nvkm_clk *clk, unsigned long *flags)
300 {
301 	struct nvkm_fifo *pfifo = nvkm_fifo(clk);
302 
303 	/* halt and idle execution engines */
304 	nv_mask(clk, 0x020060, 0x00070000, 0x00000000);
305 	nv_mask(clk, 0x002504, 0x00000001, 0x00000001);
306 	/* Wait until the interrupt handler is finished */
307 	if (!nv_wait(clk, 0x000100, 0xffffffff, 0x00000000))
308 		return -EBUSY;
309 
310 	if (pfifo)
311 		pfifo->pause(pfifo, flags);
312 
313 	if (!nv_wait(clk, 0x002504, 0x00000010, 0x00000010))
314 		return -EIO;
315 	if (!nv_wait(clk, 0x00251c, 0x0000003f, 0x0000003f))
316 		return -EIO;
317 
318 	return 0;
319 }
320 
321 void
322 gt215_clk_post(struct nvkm_clk *clk, unsigned long *flags)
323 {
324 	struct nvkm_fifo *pfifo = nvkm_fifo(clk);
325 
326 	if (pfifo && flags)
327 		pfifo->start(pfifo, flags);
328 
329 	nv_mask(clk, 0x002504, 0x00000001, 0x00000000);
330 	nv_mask(clk, 0x020060, 0x00070000, 0x00040000);
331 }
332 
333 static void
334 disable_clk_src(struct gt215_clk_priv *priv, u32 src)
335 {
336 	nv_mask(priv, src, 0x00000100, 0x00000000);
337 	nv_mask(priv, src, 0x00000001, 0x00000000);
338 }
339 
340 static void
341 prog_pll(struct gt215_clk_priv *priv, int clk, u32 pll, int idx)
342 {
343 	struct gt215_clk_info *info = &priv->eng[idx];
344 	const u32 src0 = 0x004120 + (clk * 4);
345 	const u32 src1 = 0x004160 + (clk * 4);
346 	const u32 ctrl = pll + 0;
347 	const u32 coef = pll + 4;
348 	u32 bypass;
349 
350 	if (info->pll) {
351 		/* Always start from a non-PLL clock */
352 		bypass = nv_rd32(priv, ctrl)  & 0x00000008;
353 		if (!bypass) {
354 			nv_mask(priv, src1, 0x00000101, 0x00000101);
355 			nv_mask(priv, ctrl, 0x00000008, 0x00000008);
356 			udelay(20);
357 		}
358 
359 		nv_mask(priv, src0, 0x003f3141, 0x00000101 | info->clk);
360 		nv_wr32(priv, coef, info->pll);
361 		nv_mask(priv, ctrl, 0x00000015, 0x00000015);
362 		nv_mask(priv, ctrl, 0x00000010, 0x00000000);
363 		if (!nv_wait(priv, ctrl, 0x00020000, 0x00020000)) {
364 			nv_mask(priv, ctrl, 0x00000010, 0x00000010);
365 			nv_mask(priv, src0, 0x00000101, 0x00000000);
366 			return;
367 		}
368 		nv_mask(priv, ctrl, 0x00000010, 0x00000010);
369 		nv_mask(priv, ctrl, 0x00000008, 0x00000000);
370 		disable_clk_src(priv, src1);
371 	} else {
372 		nv_mask(priv, src1, 0x003f3141, 0x00000101 | info->clk);
373 		nv_mask(priv, ctrl, 0x00000018, 0x00000018);
374 		udelay(20);
375 		nv_mask(priv, ctrl, 0x00000001, 0x00000000);
376 		disable_clk_src(priv, src0);
377 	}
378 }
379 
380 static void
381 prog_clk(struct gt215_clk_priv *priv, int clk, int idx)
382 {
383 	struct gt215_clk_info *info = &priv->eng[idx];
384 	nv_mask(priv, 0x004120 + (clk * 4), 0x003f3141, 0x00000101 | info->clk);
385 }
386 
387 static void
388 prog_host(struct gt215_clk_priv *priv)
389 {
390 	struct gt215_clk_info *info = &priv->eng[nv_clk_src_host];
391 	u32 hsrc = (nv_rd32(priv, 0xc040));
392 
393 	switch (info->host_out) {
394 	case NVA3_HOST_277:
395 		if ((hsrc & 0x30000000) == 0) {
396 			nv_wr32(priv, 0xc040, hsrc | 0x20000000);
397 			disable_clk_src(priv, 0x4194);
398 		}
399 		break;
400 	case NVA3_HOST_CLK:
401 		prog_clk(priv, 0x1d, nv_clk_src_host);
402 		if ((hsrc & 0x30000000) >= 0x20000000) {
403 			nv_wr32(priv, 0xc040, hsrc & ~0x30000000);
404 		}
405 		break;
406 	default:
407 		break;
408 	}
409 
410 	/* This seems to be a clock gating factor on idle, always set to 64 */
411 	nv_wr32(priv, 0xc044, 0x3e);
412 }
413 
414 static void
415 prog_core(struct gt215_clk_priv *priv, int idx)
416 {
417 	struct gt215_clk_info *info = &priv->eng[idx];
418 	u32 fb_delay = nv_rd32(priv, 0x10002c);
419 
420 	if (fb_delay < info->fb_delay)
421 		nv_wr32(priv, 0x10002c, info->fb_delay);
422 
423 	prog_pll(priv, 0x00, 0x004200, idx);
424 
425 	if (fb_delay > info->fb_delay)
426 		nv_wr32(priv, 0x10002c, info->fb_delay);
427 }
428 
429 static int
430 gt215_clk_calc(struct nvkm_clk *clk, struct nvkm_cstate *cstate)
431 {
432 	struct gt215_clk_priv *priv = (void *)clk;
433 	struct gt215_clk_info *core = &priv->eng[nv_clk_src_core];
434 	int ret;
435 
436 	if ((ret = calc_clk(priv, cstate, 0x10, 0x4200, nv_clk_src_core)) ||
437 	    (ret = calc_clk(priv, cstate, 0x11, 0x4220, nv_clk_src_shader)) ||
438 	    (ret = calc_clk(priv, cstate, 0x20, 0x0000, nv_clk_src_disp)) ||
439 	    (ret = calc_clk(priv, cstate, 0x21, 0x0000, nv_clk_src_vdec)) ||
440 	    (ret = calc_host(priv, cstate)))
441 		return ret;
442 
443 	/* XXX: Should be reading the highest bit in the VBIOS clock to decide
444 	 * whether to use a PLL or not... but using a PLL defeats the purpose */
445 	if (core->pll) {
446 		ret = gt215_clk_info(clk, 0x10,
447 				     cstate->domain[nv_clk_src_core_intm],
448 				     &priv->eng[nv_clk_src_core_intm]);
449 		if (ret < 0)
450 			return ret;
451 	}
452 
453 	return 0;
454 }
455 
456 static int
457 gt215_clk_prog(struct nvkm_clk *clk)
458 {
459 	struct gt215_clk_priv *priv = (void *)clk;
460 	struct gt215_clk_info *core = &priv->eng[nv_clk_src_core];
461 	int ret = 0;
462 	unsigned long flags;
463 	unsigned long *f = &flags;
464 
465 	ret = gt215_clk_pre(clk, f);
466 	if (ret)
467 		goto out;
468 
469 	if (core->pll)
470 		prog_core(priv, nv_clk_src_core_intm);
471 
472 	prog_core(priv,  nv_clk_src_core);
473 	prog_pll(priv, 0x01, 0x004220, nv_clk_src_shader);
474 	prog_clk(priv, 0x20, nv_clk_src_disp);
475 	prog_clk(priv, 0x21, nv_clk_src_vdec);
476 	prog_host(priv);
477 
478 out:
479 	if (ret == -EBUSY)
480 		f = NULL;
481 
482 	gt215_clk_post(clk, f);
483 	return ret;
484 }
485 
486 static void
487 gt215_clk_tidy(struct nvkm_clk *clk)
488 {
489 }
490 
491 static struct nvkm_domain
492 gt215_domain[] = {
493 	{ nv_clk_src_crystal  , 0xff },
494 	{ nv_clk_src_core     , 0x00, 0, "core", 1000 },
495 	{ nv_clk_src_shader   , 0x01, 0, "shader", 1000 },
496 	{ nv_clk_src_mem      , 0x02, 0, "memory", 1000 },
497 	{ nv_clk_src_vdec     , 0x03 },
498 	{ nv_clk_src_disp     , 0x04 },
499 	{ nv_clk_src_host     , 0x05 },
500 	{ nv_clk_src_core_intm, 0x06 },
501 	{ nv_clk_src_max }
502 };
503 
504 static int
505 gt215_clk_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
506 	       struct nvkm_oclass *oclass, void *data, u32 size,
507 	       struct nvkm_object **pobject)
508 {
509 	struct gt215_clk_priv *priv;
510 	int ret;
511 
512 	ret = nvkm_clk_create(parent, engine, oclass, gt215_domain,
513 			      NULL, 0, true, &priv);
514 	*pobject = nv_object(priv);
515 	if (ret)
516 		return ret;
517 
518 	priv->base.read = gt215_clk_read;
519 	priv->base.calc = gt215_clk_calc;
520 	priv->base.prog = gt215_clk_prog;
521 	priv->base.tidy = gt215_clk_tidy;
522 	return 0;
523 }
524 
525 struct nvkm_oclass
526 gt215_clk_oclass = {
527 	.handle = NV_SUBDEV(CLK, 0xa3),
528 	.ofuncs = &(struct nvkm_ofuncs) {
529 		.ctor = gt215_clk_ctor,
530 		.dtor = _nvkm_clk_dtor,
531 		.init = _nvkm_clk_init,
532 		.fini = _nvkm_clk_fini,
533 	},
534 };
535