mcp77.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
24
#define mcp77_clk(p) container_of((p), struct mcp77_clk, base)
25 26
#include "gt215.h"
#include "pll.h"
27 28 29 30 31

#include <subdev/bios.h>
#include <subdev/bios/pll.h>
#include <subdev/timer.h>

B
Ben Skeggs 已提交
32
struct mcp77_clk {
33
	struct nvkm_clk base;
34 35 36 37 38 39 40 41
	enum nv_clk_src csrc, ssrc, vsrc;
	u32 cctrl, sctrl;
	u32 ccoef, scoef;
	u32 cpost, spost;
	u32 vdiv;
};

static u32
B
Ben Skeggs 已提交
42
read_div(struct mcp77_clk *clk)
43
{
44 45
	struct nvkm_device *device = clk->base.subdev.device;
	return nvkm_rd32(device, 0x004600);
46 47 48
}

static u32
B
Ben Skeggs 已提交
49
read_pll(struct mcp77_clk *clk, u32 base)
50
{
51 52 53
	struct nvkm_device *device = clk->base.subdev.device;
	u32 ctrl = nvkm_rd32(device, base + 0);
	u32 coef = nvkm_rd32(device, base + 4);
54
	u32 ref = nvkm_clk_read(&clk->base, nv_clk_src_href);
55 56 57 58 59 60
	u32 post_div = 0;
	u32 clock = 0;
	int N1, M1;

	switch (base){
	case 0x4020:
61
		post_div = 1 << ((nvkm_rd32(device, 0x4070) & 0x000f0000) >> 16);
62 63
		break;
	case 0x4028:
64
		post_div = (nvkm_rd32(device, 0x4040) & 0x000f0000) >> 16;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		break;
	default:
		break;
	}

	N1 = (coef & 0x0000ff00) >> 8;
	M1 = (coef & 0x000000ff);
	if ((ctrl & 0x80000000) && M1) {
		clock = ref * N1 / M1;
		clock = clock / post_div;
	}

	return clock;
}

static int
81
mcp77_clk_read(struct nvkm_clk *base, enum nv_clk_src src)
82
{
83
	struct mcp77_clk *clk = mcp77_clk(base);
84 85
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
86
	u32 mast = nvkm_rd32(device, 0x00c054);
87 88 89 90
	u32 P = 0;

	switch (src) {
	case nv_clk_src_crystal:
91
		return device->crystal;
92 93 94
	case nv_clk_src_href:
		return 100000; /* PCIE reference clock */
	case nv_clk_src_hclkm4:
95
		return nvkm_clk_read(&clk->base, nv_clk_src_href) * 4;
96
	case nv_clk_src_hclkm2d3:
97
		return nvkm_clk_read(&clk->base, nv_clk_src_href) * 2 / 3;
98 99
	case nv_clk_src_host:
		switch (mast & 0x000c0000) {
100
		case 0x00000000: return nvkm_clk_read(&clk->base, nv_clk_src_hclkm2d3);
101
		case 0x00040000: break;
102 103
		case 0x00080000: return nvkm_clk_read(&clk->base, nv_clk_src_hclkm4);
		case 0x000c0000: return nvkm_clk_read(&clk->base, nv_clk_src_cclk);
104 105 106
		}
		break;
	case nv_clk_src_core:
107
		P = (nvkm_rd32(device, 0x004028) & 0x00070000) >> 16;
108 109

		switch (mast & 0x00000003) {
110
		case 0x00000000: return nvkm_clk_read(&clk->base, nv_clk_src_crystal) >> P;
111
		case 0x00000001: return 0;
112
		case 0x00000002: return nvkm_clk_read(&clk->base, nv_clk_src_hclkm4) >> P;
113 114 115 116 117
		case 0x00000003: return read_pll(clk, 0x004028) >> P;
		}
		break;
	case nv_clk_src_cclk:
		if ((mast & 0x03000000) != 0x03000000)
118
			return nvkm_clk_read(&clk->base, nv_clk_src_core);
119 120

		if ((mast & 0x00000200) == 0x00000000)
121
			return nvkm_clk_read(&clk->base, nv_clk_src_core);
122 123

		switch (mast & 0x00000c00) {
124 125 126
		case 0x00000000: return nvkm_clk_read(&clk->base, nv_clk_src_href);
		case 0x00000400: return nvkm_clk_read(&clk->base, nv_clk_src_hclkm4);
		case 0x00000800: return nvkm_clk_read(&clk->base, nv_clk_src_hclkm2d3);
127 128 129
		default: return 0;
		}
	case nv_clk_src_shader:
130
		P = (nvkm_rd32(device, 0x004020) & 0x00070000) >> 16;
131 132 133
		switch (mast & 0x00000030) {
		case 0x00000000:
			if (mast & 0x00000040)
134 135
				return nvkm_clk_read(&clk->base, nv_clk_src_href) >> P;
			return nvkm_clk_read(&clk->base, nv_clk_src_crystal) >> P;
136 137 138 139 140 141 142 143 144 145 146 147 148
		case 0x00000010: break;
		case 0x00000020: return read_pll(clk, 0x004028) >> P;
		case 0x00000030: return read_pll(clk, 0x004020) >> P;
		}
		break;
	case nv_clk_src_mem:
		return 0;
		break;
	case nv_clk_src_vdec:
		P = (read_div(clk) & 0x00000700) >> 8;

		switch (mast & 0x00400000) {
		case 0x00400000:
149
			return nvkm_clk_read(&clk->base, nv_clk_src_core) >> P;
150 151 152 153 154 155 156 157 158 159
			break;
		default:
			return 500000 >> P;
			break;
		}
		break;
	default:
		break;
	}

160
	nvkm_debug(subdev, "unknown clock source %d %08x\n", src, mast);
161 162 163 164
	return 0;
}

static u32
B
Ben Skeggs 已提交
165
calc_pll(struct mcp77_clk *clk, u32 reg,
166 167
	 u32 clock, int *N, int *M, int *P)
{
168
	struct nvkm_subdev *subdev = &clk->base.subdev;
169 170 171
	struct nvbios_pll pll;
	int ret;

172
	ret = nvbios_pll_parse(subdev->device->bios, reg, &pll);
173 174 175 176
	if (ret)
		return 0;

	pll.vco2.max_freq = 0;
177
	pll.refclk = nvkm_clk_read(&clk->base, nv_clk_src_href);
178 179 180
	if (!pll.refclk)
		return 0;

181
	return nv04_pll_calc(subdev, &pll, clock, N, M, NULL, NULL, P);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

static inline u32
calc_P(u32 src, u32 target, int *div)
{
	u32 clk0 = src, clk1 = src;
	for (*div = 0; *div <= 7; (*div)++) {
		if (clk0 <= target) {
			clk1 = clk0 << (*div ? 1 : 0);
			break;
		}
		clk0 >>= 1;
	}

	if (target - clk0 <= clk1 - target)
		return clk0;
	(*div)--;
	return clk1;
}

static int
203
mcp77_clk_calc(struct nvkm_clk *base, struct nvkm_cstate *cstate)
204
{
205
	struct mcp77_clk *clk = mcp77_clk(base);
206 207 208
	const int shader = cstate->domain[nv_clk_src_shader];
	const int core = cstate->domain[nv_clk_src_core];
	const int vdec = cstate->domain[nv_clk_src_vdec];
209
	struct nvkm_subdev *subdev = &clk->base.subdev;
210 211 212 213 214
	u32 out = 0, clock = 0;
	int N, M, P1, P2 = 0;
	int divs = 0;

	/* cclk: find suitable source, disable PLL if we can */
215 216
	if (core < nvkm_clk_read(&clk->base, nv_clk_src_hclkm4))
		out = calc_P(nvkm_clk_read(&clk->base, nv_clk_src_hclkm4), core, &divs);
217 218

	/* Calculate clock * 2, so shader clock can use it too */
B
Ben Skeggs 已提交
219
	clock = calc_pll(clk, 0x4028, (core << 1), &N, &M, &P1);
220

221
	if (abs(core - out) <= abs(core - (clock >> 1))) {
B
Ben Skeggs 已提交
222 223
		clk->csrc = nv_clk_src_hclkm4;
		clk->cctrl = divs << 16;
224 225 226 227 228 229 230 231 232
	} else {
		/* NVCTRL is actually used _after_ NVPOST, and after what we
		 * call NVPLL. To make matters worse, NVPOST is an integer
		 * divider instead of a right-shift number. */
		if(P1 > 2) {
			P2 = P1 - 2;
			P1 = 2;
		}

B
Ben Skeggs 已提交
233 234
		clk->csrc = nv_clk_src_core;
		clk->ccoef = (N << 8) | M;
235

B
Ben Skeggs 已提交
236 237
		clk->cctrl = (P2 + 1) << 16;
		clk->cpost = (1 << P1) << 16;
238 239 240 241
	}

	/* sclk: nvpll + divisor, href or spll */
	out = 0;
242
	if (shader == nvkm_clk_read(&clk->base, nv_clk_src_href)) {
B
Ben Skeggs 已提交
243
		clk->ssrc = nv_clk_src_href;
244
	} else {
B
Ben Skeggs 已提交
245 246
		clock = calc_pll(clk, 0x4020, shader, &N, &M, &P1);
		if (clk->csrc == nv_clk_src_core)
247 248 249 250 251
			out = calc_P((core << 1), shader, &divs);

		if (abs(shader - out) <=
		    abs(shader - clock) &&
		   (divs + P2) <= 7) {
B
Ben Skeggs 已提交
252 253
			clk->ssrc = nv_clk_src_core;
			clk->sctrl = (divs + P2) << 16;
254
		} else {
B
Ben Skeggs 已提交
255 256 257
			clk->ssrc = nv_clk_src_shader;
			clk->scoef = (N << 8) | M;
			clk->sctrl = P1 << 16;
258 259 260 261 262 263
		}
	}

	/* vclk */
	out = calc_P(core, vdec, &divs);
	clock = calc_P(500000, vdec, &P1);
264
	if(abs(vdec - out) <= abs(vdec - clock)) {
B
Ben Skeggs 已提交
265 266
		clk->vsrc = nv_clk_src_cclk;
		clk->vdiv = divs << 16;
267
	} else {
B
Ben Skeggs 已提交
268 269
		clk->vsrc = nv_clk_src_vdec;
		clk->vdiv = P1 << 16;
270 271 272
	}

	/* Print strategy! */
273 274 275 276 277
	nvkm_debug(subdev, "nvpll: %08x %08x %08x\n",
		   clk->ccoef, clk->cpost, clk->cctrl);
	nvkm_debug(subdev, " spll: %08x %08x %08x\n",
		   clk->scoef, clk->spost, clk->sctrl);
	nvkm_debug(subdev, " vdiv: %08x\n", clk->vdiv);
B
Ben Skeggs 已提交
278
	if (clk->csrc == nv_clk_src_hclkm4)
279
		nvkm_debug(subdev, "core: hrefm4\n");
280
	else
281
		nvkm_debug(subdev, "core: nvpll\n");
282

B
Ben Skeggs 已提交
283
	if (clk->ssrc == nv_clk_src_hclkm4)
284
		nvkm_debug(subdev, "shader: hrefm4\n");
B
Ben Skeggs 已提交
285
	else if (clk->ssrc == nv_clk_src_core)
286
		nvkm_debug(subdev, "shader: nvpll\n");
287
	else
288
		nvkm_debug(subdev, "shader: spll\n");
289

B
Ben Skeggs 已提交
290
	if (clk->vsrc == nv_clk_src_hclkm4)
291
		nvkm_debug(subdev, "vdec: 500MHz\n");
292
	else
293
		nvkm_debug(subdev, "vdec: core\n");
294 295 296 297 298

	return 0;
}

static int
299
mcp77_clk_prog(struct nvkm_clk *base)
300
{
301
	struct mcp77_clk *clk = mcp77_clk(base);
302 303
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
304
	u32 pllmask = 0, mast;
305
	unsigned long flags;
306 307
	unsigned long *f = &flags;
	int ret = 0;
308

B
Ben Skeggs 已提交
309
	ret = gt215_clk_pre(&clk->base, f);
310 311
	if (ret)
		goto out;
312 313

	/* First switch to safe clocks: href */
314
	mast = nvkm_mask(device, 0xc054, 0x03400e70, 0x03400640);
315 316 317
	mast &= ~0x00400e73;
	mast |= 0x03000000;

B
Ben Skeggs 已提交
318
	switch (clk->csrc) {
319
	case nv_clk_src_hclkm4:
320
		nvkm_mask(device, 0x4028, 0x00070000, clk->cctrl);
321 322 323
		mast |= 0x00000002;
		break;
	case nv_clk_src_core:
324 325 326
		nvkm_wr32(device, 0x402c, clk->ccoef);
		nvkm_wr32(device, 0x4028, 0x80000000 | clk->cctrl);
		nvkm_wr32(device, 0x4040, clk->cpost);
327 328 329 330
		pllmask |= (0x3 << 8);
		mast |= 0x00000003;
		break;
	default:
331
		nvkm_warn(subdev, "Reclocking failed: unknown core clock\n");
332 333 334
		goto resume;
	}

B
Ben Skeggs 已提交
335
	switch (clk->ssrc) {
336
	case nv_clk_src_href:
337
		nvkm_mask(device, 0x4020, 0x00070000, 0x00000000);
338 339 340
		/* mast |= 0x00000000; */
		break;
	case nv_clk_src_core:
341
		nvkm_mask(device, 0x4020, 0x00070000, clk->sctrl);
342 343 344
		mast |= 0x00000020;
		break;
	case nv_clk_src_shader:
345 346 347
		nvkm_wr32(device, 0x4024, clk->scoef);
		nvkm_wr32(device, 0x4020, 0x80000000 | clk->sctrl);
		nvkm_wr32(device, 0x4070, clk->spost);
348 349 350 351
		pllmask |= (0x3 << 12);
		mast |= 0x00000030;
		break;
	default:
352
		nvkm_warn(subdev, "Reclocking failed: unknown sclk clock\n");
353 354 355
		goto resume;
	}

356 357 358 359 360
	if (nvkm_msec(device, 2000,
		u32 tmp = nvkm_rd32(device, 0x004080) & pllmask;
		if (tmp == pllmask)
			break;
	) < 0)
361 362
		goto resume;

B
Ben Skeggs 已提交
363
	switch (clk->vsrc) {
364 365 366
	case nv_clk_src_cclk:
		mast |= 0x00400000;
	default:
367
		nvkm_wr32(device, 0x4600, clk->vdiv);
368 369
	}

370
	nvkm_wr32(device, 0xc054, mast);
371 372 373

resume:
	/* Disable some PLLs and dividers when unused */
B
Ben Skeggs 已提交
374
	if (clk->csrc != nv_clk_src_core) {
375 376
		nvkm_wr32(device, 0x4040, 0x00000000);
		nvkm_mask(device, 0x4028, 0x80000000, 0x00000000);
377 378
	}

B
Ben Skeggs 已提交
379
	if (clk->ssrc != nv_clk_src_shader) {
380 381
		nvkm_wr32(device, 0x4070, 0x00000000);
		nvkm_mask(device, 0x4020, 0x80000000, 0x00000000);
382 383
	}

384 385 386 387
out:
	if (ret == -EBUSY)
		f = NULL;

B
Ben Skeggs 已提交
388
	gt215_clk_post(&clk->base, f);
389 390 391 392
	return ret;
}

static void
393
mcp77_clk_tidy(struct nvkm_clk *base)
394 395 396
{
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410
static const struct nvkm_clk_func
mcp77_clk = {
	.read = mcp77_clk_read,
	.calc = mcp77_clk_calc,
	.prog = mcp77_clk_prog,
	.tidy = mcp77_clk_tidy,
	.domains = {
		{ nv_clk_src_crystal, 0xff },
		{ nv_clk_src_href   , 0xff },
		{ nv_clk_src_core   , 0xff, 0, "core", 1000 },
		{ nv_clk_src_shader , 0xff, 0, "shader", 1000 },
		{ nv_clk_src_vdec   , 0xff, 0, "vdec", 1000 },
		{ nv_clk_src_max }
	}
411 412
};

413 414
int
mcp77_clk_new(struct nvkm_device *device, int index, struct nvkm_clk **pclk)
415
{
B
Ben Skeggs 已提交
416
	struct mcp77_clk *clk;
417

418 419 420
	if (!(clk = kzalloc(sizeof(*clk), GFP_KERNEL)))
		return -ENOMEM;
	*pclk = &clk->base;
421

422
	return nvkm_clk_ctor(&mcp77_clk, device, index, true, &clk->base);
423
}