gk20a.c 17.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Shamelessly ripped off from ChromeOS's gk20a/clk_pllg.c
 *
 */
25 26 27
#define gk20a_clk(p) container_of((p), struct gk20a_clk, base)
#include "priv.h"

28
#include <core/tegra.h>
29 30
#include <subdev/timer.h>

31 32
#define KHZ (1000)
#define MHZ (KHZ * 1000)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

#define MASK(w)	((1 << w) - 1)

#define SYS_GPCPLL_CFG_BASE			0x00137000
#define GPC_BCASE_GPCPLL_CFG_BASE		0x00132800

#define GPCPLL_CFG		(SYS_GPCPLL_CFG_BASE + 0)
#define GPCPLL_CFG_ENABLE	BIT(0)
#define GPCPLL_CFG_IDDQ		BIT(1)
#define GPCPLL_CFG_LOCK_DET_OFF	BIT(4)
#define GPCPLL_CFG_LOCK		BIT(17)

#define GPCPLL_COEFF		(SYS_GPCPLL_CFG_BASE + 4)
#define GPCPLL_COEFF_M_SHIFT	0
#define GPCPLL_COEFF_M_WIDTH	8
#define GPCPLL_COEFF_N_SHIFT	8
#define GPCPLL_COEFF_N_WIDTH	8
#define GPCPLL_COEFF_P_SHIFT	16
#define GPCPLL_COEFF_P_WIDTH	6

#define GPCPLL_CFG2			(SYS_GPCPLL_CFG_BASE + 0xc)
#define GPCPLL_CFG2_SETUP2_SHIFT	16
#define GPCPLL_CFG2_PLL_STEPA_SHIFT	24

#define GPCPLL_CFG3			(SYS_GPCPLL_CFG_BASE + 0x18)
#define GPCPLL_CFG3_PLL_STEPB_SHIFT	16

#define GPCPLL_NDIV_SLOWDOWN			(SYS_GPCPLL_CFG_BASE + 0x1c)
#define GPCPLL_NDIV_SLOWDOWN_NDIV_LO_SHIFT	0
#define GPCPLL_NDIV_SLOWDOWN_NDIV_MID_SHIFT	8
#define GPCPLL_NDIV_SLOWDOWN_STEP_SIZE_LO2MID_SHIFT	16
#define GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT	22
#define GPCPLL_NDIV_SLOWDOWN_EN_DYNRAMP_SHIFT	31

#define SEL_VCO				(SYS_GPCPLL_CFG_BASE + 0x100)
#define SEL_VCO_GPC2CLK_OUT_SHIFT	0

#define GPC2CLK_OUT			(SYS_GPCPLL_CFG_BASE + 0x250)
#define GPC2CLK_OUT_SDIV14_INDIV4_WIDTH	1
#define GPC2CLK_OUT_SDIV14_INDIV4_SHIFT	31
#define GPC2CLK_OUT_SDIV14_INDIV4_MODE	1
#define GPC2CLK_OUT_VCODIV_WIDTH	6
#define GPC2CLK_OUT_VCODIV_SHIFT	8
#define GPC2CLK_OUT_VCODIV1		0
#define GPC2CLK_OUT_VCODIV_MASK		(MASK(GPC2CLK_OUT_VCODIV_WIDTH) << \
					GPC2CLK_OUT_VCODIV_SHIFT)
#define	GPC2CLK_OUT_BYPDIV_WIDTH	6
#define GPC2CLK_OUT_BYPDIV_SHIFT	0
#define GPC2CLK_OUT_BYPDIV31		0x3c
#define GPC2CLK_OUT_INIT_MASK	((MASK(GPC2CLK_OUT_SDIV14_INDIV4_WIDTH) << \
		GPC2CLK_OUT_SDIV14_INDIV4_SHIFT)\
		| (MASK(GPC2CLK_OUT_VCODIV_WIDTH) << GPC2CLK_OUT_VCODIV_SHIFT)\
		| (MASK(GPC2CLK_OUT_BYPDIV_WIDTH) << GPC2CLK_OUT_BYPDIV_SHIFT))
#define GPC2CLK_OUT_INIT_VAL	((GPC2CLK_OUT_SDIV14_INDIV4_MODE << \
		GPC2CLK_OUT_SDIV14_INDIV4_SHIFT) \
		| (GPC2CLK_OUT_VCODIV1 << GPC2CLK_OUT_VCODIV_SHIFT) \
		| (GPC2CLK_OUT_BYPDIV31 << GPC2CLK_OUT_BYPDIV_SHIFT))

#define GPC_BCAST_NDIV_SLOWDOWN_DEBUG	(GPC_BCASE_GPCPLL_CFG_BASE + 0xa0)
#define GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_SHIFT	24
#define GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_MASK \
	    (0x1 << GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_SHIFT)

96
static const u8 _pl_to_div[] = {
97 98 99 100
/* PL:   0, 1, 2, 3, 4, 5, 6,  7,  8,  9, 10, 11, 12, 13, 14 */
/* p: */ 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 12, 16, 20, 24, 32,
};

101
/* All frequencies in Khz */
102 103 104 105 106 107 108
struct gk20a_clk_pllg_params {
	u32 min_vco, max_vco;
	u32 min_u, max_u;
	u32 min_m, max_m;
	u32 min_n, max_n;
	u32 min_pl, max_pl;
};
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static u32 pl_to_div(u32 pl)
{
	if (pl >= ARRAY_SIZE(_pl_to_div))
		return 1;

	return _pl_to_div[pl];
}

static u32 div_to_pl(u32 div)
{
	u32 pl;

	for (pl = 0; pl < ARRAY_SIZE(_pl_to_div) - 1; pl++) {
		if (_pl_to_div[pl] >= div)
			return pl;
	}

	return ARRAY_SIZE(_pl_to_div) - 1;
}
128 129

static const struct gk20a_clk_pllg_params gk20a_pllg_params = {
130 131
	.min_vco = 1000000, .max_vco = 2064000,
	.min_u = 12000, .max_u = 38000,
132 133 134 135 136
	.min_m = 1, .max_m = 255,
	.min_n = 8, .max_n = 255,
	.min_pl = 1, .max_pl = 32,
};

137 138 139 140 141 142
struct gk20a_pll {
	u32 m;
	u32 n;
	u32 pl;
};

B
Ben Skeggs 已提交
143
struct gk20a_clk {
144
	struct nvkm_clk base;
145
	const struct gk20a_clk_pllg_params *params;
146
	struct gk20a_pll pll;
147
	u32 parent_rate;
148 149 150

	u32 (*div_to_pl)(u32);
	u32 (*pl_to_div)(u32);
151 152 153
};

static void
154
gk20a_pllg_read_mnp(struct gk20a_clk *clk, struct gk20a_pll *pll)
155
{
156
	struct nvkm_device *device = clk->base.subdev.device;
157 158
	u32 val;

159
	val = nvkm_rd32(device, GPCPLL_COEFF);
160 161 162
	pll->m = (val >> GPCPLL_COEFF_M_SHIFT) & MASK(GPCPLL_COEFF_M_WIDTH);
	pll->n = (val >> GPCPLL_COEFF_N_SHIFT) & MASK(GPCPLL_COEFF_N_WIDTH);
	pll->pl = (val >> GPCPLL_COEFF_P_SHIFT) & MASK(GPCPLL_COEFF_P_WIDTH);
163 164 165
}

static u32
B
Ben Skeggs 已提交
166
gk20a_pllg_calc_rate(struct gk20a_clk *clk)
167 168 169 170
{
	u32 rate;
	u32 divider;

171
	rate = clk->parent_rate * clk->pll.n;
172
	divider = clk->pll.m * clk->pl_to_div(clk->pll.pl);
173

174
	return rate / divider / 2;
175 176 177
}

static int
B
Ben Skeggs 已提交
178
gk20a_pllg_calc_mnp(struct gk20a_clk *clk, unsigned long rate)
179
{
180
	struct nvkm_subdev *subdev = &clk->base.subdev;
181 182 183
	u32 target_clk_f, ref_clk_f, target_freq;
	u32 min_vco_f, max_vco_f;
	u32 low_pl, high_pl, best_pl;
184
	u32 target_vco_f;
185
	u32 best_m, best_n;
186
	u32 best_delta = ~0;
187 188
	u32 pl;

189 190
	target_clk_f = rate * 2 / KHZ;
	ref_clk_f = clk->parent_rate / KHZ;
191

B
Ben Skeggs 已提交
192 193 194 195 196
	max_vco_f = clk->params->max_vco;
	min_vco_f = clk->params->min_vco;
	best_m = clk->params->max_m;
	best_n = clk->params->min_n;
	best_pl = clk->params->min_pl;
197 198 199 200 201 202 203

	target_vco_f = target_clk_f + target_clk_f / 50;
	if (max_vco_f < target_vco_f)
		max_vco_f = target_vco_f;

	/* min_pl <= high_pl <= max_pl */
	high_pl = (max_vco_f + target_vco_f - 1) / target_vco_f;
B
Ben Skeggs 已提交
204 205
	high_pl = min(high_pl, clk->params->max_pl);
	high_pl = max(high_pl, clk->params->min_pl);
206
	high_pl = clk->div_to_pl(high_pl);
207 208 209

	/* min_pl <= low_pl <= max_pl */
	low_pl = min_vco_f / target_vco_f;
B
Ben Skeggs 已提交
210 211
	low_pl = min(low_pl, clk->params->max_pl);
	low_pl = max(low_pl, clk->params->min_pl);
212
	low_pl = clk->div_to_pl(low_pl);
213

214
	nvkm_debug(subdev, "low_PL %d(div%d), high_PL %d(div%d)", low_pl,
215
		   clk->pl_to_div(low_pl), high_pl, clk->pl_to_div(high_pl));
216 217 218

	/* Select lowest possible VCO */
	for (pl = low_pl; pl <= high_pl; pl++) {
219 220
		u32 m, n, n2;

221 222
		target_vco_f = target_clk_f * clk->pl_to_div(pl);

B
Ben Skeggs 已提交
223
		for (m = clk->params->min_m; m <= clk->params->max_m; m++) {
224 225
			u32 u_f, vco_f;

226 227
			u_f = ref_clk_f / m;

B
Ben Skeggs 已提交
228
			if (u_f < clk->params->min_u)
229
				break;
B
Ben Skeggs 已提交
230
			if (u_f > clk->params->max_u)
231 232 233 234 235
				continue;

			n = (target_vco_f * m) / ref_clk_f;
			n2 = ((target_vco_f * m) + (ref_clk_f - 1)) / ref_clk_f;

B
Ben Skeggs 已提交
236
			if (n > clk->params->max_n)
237 238 239
				break;

			for (; n <= n2; n++) {
B
Ben Skeggs 已提交
240
				if (n < clk->params->min_n)
241
					continue;
B
Ben Skeggs 已提交
242
				if (n > clk->params->max_n)
243 244 245 246 247
					break;

				vco_f = ref_clk_f * n / m;

				if (vco_f >= min_vco_f && vco_f <= max_vco_f) {
248 249
					u32 delta, lwv;

250 251
					lwv = (vco_f + (clk->pl_to_div(pl) / 2))
						/ clk->pl_to_div(pl);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
					delta = abs(lwv - target_clk_f);

					if (delta < best_delta) {
						best_delta = delta;
						best_m = m;
						best_n = n;
						best_pl = pl;

						if (best_delta == 0)
							goto found_match;
					}
				}
			}
		}
	}

found_match:
	WARN_ON(best_delta == ~0);

	if (best_delta != 0)
272 273
		nvkm_debug(subdev,
			   "no best match for target @ %dMHz on gpc_pll",
274
			   target_clk_f / KHZ);
275

276 277 278
	clk->pll.m = best_m;
	clk->pll.n = best_n;
	clk->pll.pl = best_pl;
279

280
	target_freq = gk20a_pllg_calc_rate(clk);
281

282 283
	nvkm_debug(subdev,
		   "actual target freq %d MHz, M %d, N %d, PL %d(div%d)\n",
284
		   target_freq / MHZ, clk->pll.m, clk->pll.n, clk->pll.pl,
285
		   clk->pl_to_div(clk->pll.pl));
286 287 288 289
	return 0;
}

static int
B
Ben Skeggs 已提交
290
gk20a_pllg_slide(struct gk20a_clk *clk, u32 n)
291
{
292 293
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
294 295 296 297
	u32 val;
	int ramp_timeout;

	/* get old coefficients */
298
	val = nvkm_rd32(device, GPCPLL_COEFF);
299 300 301 302 303
	/* do nothing if NDIV is the same */
	if (n == ((val >> GPCPLL_COEFF_N_SHIFT) & MASK(GPCPLL_COEFF_N_WIDTH)))
		return 0;

	/* setup */
304
	nvkm_mask(device, GPCPLL_CFG2, 0xff << GPCPLL_CFG2_PLL_STEPA_SHIFT,
305
		0x2b << GPCPLL_CFG2_PLL_STEPA_SHIFT);
306
	nvkm_mask(device, GPCPLL_CFG3, 0xff << GPCPLL_CFG3_PLL_STEPB_SHIFT,
307 308 309
		0xb << GPCPLL_CFG3_PLL_STEPB_SHIFT);

	/* pll slowdown mode */
310
	nvkm_mask(device, GPCPLL_NDIV_SLOWDOWN,
311 312 313 314
		BIT(GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT),
		BIT(GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT));

	/* new ndiv ready for ramp */
315
	val = nvkm_rd32(device, GPCPLL_COEFF);
316 317 318
	val &= ~(MASK(GPCPLL_COEFF_N_WIDTH) << GPCPLL_COEFF_N_SHIFT);
	val |= (n & MASK(GPCPLL_COEFF_N_WIDTH)) << GPCPLL_COEFF_N_SHIFT;
	udelay(1);
319
	nvkm_wr32(device, GPCPLL_COEFF, val);
320 321

	/* dynamic ramp to new ndiv */
322
	val = nvkm_rd32(device, GPCPLL_NDIV_SLOWDOWN);
323 324
	val |= 0x1 << GPCPLL_NDIV_SLOWDOWN_EN_DYNRAMP_SHIFT;
	udelay(1);
325
	nvkm_wr32(device, GPCPLL_NDIV_SLOWDOWN, val);
326 327 328

	for (ramp_timeout = 500; ramp_timeout > 0; ramp_timeout--) {
		udelay(1);
329
		val = nvkm_rd32(device, GPC_BCAST_NDIV_SLOWDOWN_DEBUG);
330 331 332 333 334
		if (val & GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_MASK)
			break;
	}

	/* exit slowdown mode */
335
	nvkm_mask(device, GPCPLL_NDIV_SLOWDOWN,
336 337
		BIT(GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT) |
		BIT(GPCPLL_NDIV_SLOWDOWN_EN_DYNRAMP_SHIFT), 0);
338
	nvkm_rd32(device, GPCPLL_NDIV_SLOWDOWN);
339 340

	if (ramp_timeout <= 0) {
341
		nvkm_error(subdev, "gpcpll dynamic ramp timeout\n");
342 343 344 345 346 347 348
		return -ETIMEDOUT;
	}

	return 0;
}

static void
349
gk20a_pllg_enable(struct gk20a_clk *clk)
350
{
351
	struct nvkm_device *device = clk->base.subdev.device;
352

353 354
	nvkm_mask(device, GPCPLL_CFG, GPCPLL_CFG_ENABLE, GPCPLL_CFG_ENABLE);
	nvkm_rd32(device, GPCPLL_CFG);
355 356 357
}

static void
358
gk20a_pllg_disable(struct gk20a_clk *clk)
359
{
360
	struct nvkm_device *device = clk->base.subdev.device;
361

362 363
	nvkm_mask(device, GPCPLL_CFG, GPCPLL_CFG_ENABLE, 0);
	nvkm_rd32(device, GPCPLL_CFG);
364 365 366
}

static int
B
Ben Skeggs 已提交
367
_gk20a_pllg_program_mnp(struct gk20a_clk *clk, bool allow_slide)
368
{
369 370
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
371
	u32 val, cfg;
372 373
	struct gk20a_pll old_pll;
	u32 n_lo;
374 375

	/* get old coefficients */
376
	gk20a_pllg_read_mnp(clk, &old_pll);
377 378

	/* do NDIV slide if there is no change in M and PL */
379
	cfg = nvkm_rd32(device, GPCPLL_CFG);
380 381 382
	if (allow_slide && clk->pll.m == old_pll.m &&
	    clk->pll.pl == old_pll.pl && (cfg & GPCPLL_CFG_ENABLE)) {
		return gk20a_pllg_slide(clk, clk->pll.n);
383 384 385 386
	}

	/* slide down to NDIV_LO */
	if (allow_slide && (cfg & GPCPLL_CFG_ENABLE)) {
387 388
		int ret;

389
		n_lo = DIV_ROUND_UP(old_pll.m * clk->params->min_vco,
390 391
				    clk->parent_rate / KHZ);
		ret = gk20a_pllg_slide(clk, n_lo);
392 393 394 395 396 397

		if (ret)
			return ret;
	}

	/* split FO-to-bypass jump in halfs by setting out divider 1:2 */
398
	nvkm_mask(device, GPC2CLK_OUT, GPC2CLK_OUT_VCODIV_MASK,
399 400 401
		0x2 << GPC2CLK_OUT_VCODIV_SHIFT);

	/* put PLL in bypass before programming it */
402
	val = nvkm_rd32(device, SEL_VCO);
403 404
	val &= ~(BIT(SEL_VCO_GPC2CLK_OUT_SHIFT));
	udelay(2);
405
	nvkm_wr32(device, SEL_VCO, val);
406 407

	/* get out from IDDQ */
408
	val = nvkm_rd32(device, GPCPLL_CFG);
409 410
	if (val & GPCPLL_CFG_IDDQ) {
		val &= ~GPCPLL_CFG_IDDQ;
411 412
		nvkm_wr32(device, GPCPLL_CFG, val);
		nvkm_rd32(device, GPCPLL_CFG);
413 414 415
		udelay(2);
	}

416
	gk20a_pllg_disable(clk);
417

418
	nvkm_debug(subdev, "%s: m=%d n=%d pl=%d\n", __func__,
419
		   clk->pll.m, clk->pll.n, clk->pll.pl);
420

421
	n_lo = DIV_ROUND_UP(clk->pll.m * clk->params->min_vco,
422
			    clk->parent_rate / KHZ);
423 424 425
	val = clk->pll.m << GPCPLL_COEFF_M_SHIFT;
	val |= (allow_slide ? n_lo : clk->pll.n) << GPCPLL_COEFF_N_SHIFT;
	val |= clk->pll.pl << GPCPLL_COEFF_P_SHIFT;
426
	nvkm_wr32(device, GPCPLL_COEFF, val);
427

428
	gk20a_pllg_enable(clk);
429

430
	val = nvkm_rd32(device, GPCPLL_CFG);
431 432
	if (val & GPCPLL_CFG_LOCK_DET_OFF) {
		val &= ~GPCPLL_CFG_LOCK_DET_OFF;
433
		nvkm_wr32(device, GPCPLL_CFG, val);
434 435
	}

436 437 438 439
	if (nvkm_usec(device, 300,
		if (nvkm_rd32(device, GPCPLL_CFG) & GPCPLL_CFG_LOCK)
			break;
	) < 0)
440 441 442
		return -ETIMEDOUT;

	/* switch to VCO mode */
443 444
	nvkm_mask(device, SEL_VCO, BIT(SEL_VCO_GPC2CLK_OUT_SHIFT),
		  BIT(SEL_VCO_GPC2CLK_OUT_SHIFT));
445 446

	/* restore out divider 1:1 */
447
	val = nvkm_rd32(device, GPC2CLK_OUT);
448 449 450 451 452 453 454 455 456 457
	if ((val & GPC2CLK_OUT_VCODIV_MASK) !=
	    (GPC2CLK_OUT_VCODIV1 << GPC2CLK_OUT_VCODIV_SHIFT)) {
		val &= ~GPC2CLK_OUT_VCODIV_MASK;
		val |= GPC2CLK_OUT_VCODIV1 << GPC2CLK_OUT_VCODIV_SHIFT;
		udelay(2);
		nvkm_wr32(device, GPC2CLK_OUT, val);
		/* Intentional 2nd write to assure linear divider operation */
		nvkm_wr32(device, GPC2CLK_OUT, val);
		nvkm_rd32(device, GPC2CLK_OUT);
	}
458 459

	/* slide up to new NDIV */
460
	return allow_slide ? gk20a_pllg_slide(clk, clk->pll.n) : 0;
461 462 463
}

static int
B
Ben Skeggs 已提交
464
gk20a_pllg_program_mnp(struct gk20a_clk *clk)
465 466 467
{
	int err;

B
Ben Skeggs 已提交
468
	err = _gk20a_pllg_program_mnp(clk, true);
469
	if (err)
B
Ben Skeggs 已提交
470
		err = _gk20a_pllg_program_mnp(clk, false);
471 472 473 474 475 476

	return err;
}

#define GK20A_CLK_GPC_MDIV 1000

477
static struct nvkm_pstate
478 479 480 481
gk20a_pstates[] = {
	{
		.base = {
			.domain[nv_clk_src_gpc] = 72000,
482
			.voltage = 0,
483 484 485 486 487
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 108000,
488
			.voltage = 1,
489 490 491 492 493
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 180000,
494
			.voltage = 2,
495 496 497 498 499
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 252000,
500
			.voltage = 3,
501 502 503 504 505
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 324000,
506
			.voltage = 4,
507 508 509 510 511
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 396000,
512
			.voltage = 5,
513 514 515 516 517
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 468000,
518
			.voltage = 6,
519 520 521 522 523
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 540000,
524
			.voltage = 7,
525 526 527 528 529
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 612000,
530
			.voltage = 8,
531 532 533 534 535
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 648000,
536
			.voltage = 9,
537 538 539 540 541
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 684000,
542
			.voltage = 10,
543 544 545 546 547
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 708000,
548
			.voltage = 11,
549 550 551 552 553
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 756000,
554
			.voltage = 12,
555 556 557 558 559
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 804000,
560
			.voltage = 13,
561 562 563 564 565
		},
	},
	{
		.base = {
			.domain[nv_clk_src_gpc] = 852000,
566
			.voltage = 14,
567 568 569 570 571
		},
	},
};

static int
572
gk20a_clk_read(struct nvkm_clk *base, enum nv_clk_src src)
573
{
574
	struct gk20a_clk *clk = gk20a_clk(base);
575 576
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
577 578 579

	switch (src) {
	case nv_clk_src_crystal:
580
		return device->crystal;
581
	case nv_clk_src_gpc:
582
		gk20a_pllg_read_mnp(clk, &clk->pll);
B
Ben Skeggs 已提交
583
		return gk20a_pllg_calc_rate(clk) / GK20A_CLK_GPC_MDIV;
584
	default:
585
		nvkm_error(subdev, "invalid clock source %d\n", src);
586 587 588 589 590
		return -EINVAL;
	}
}

static int
591
gk20a_clk_calc(struct nvkm_clk *base, struct nvkm_cstate *cstate)
592
{
593
	struct gk20a_clk *clk = gk20a_clk(base);
594

B
Ben Skeggs 已提交
595
	return gk20a_pllg_calc_mnp(clk, cstate->domain[nv_clk_src_gpc] *
596 597 598 599
					 GK20A_CLK_GPC_MDIV);
}

static int
600
gk20a_clk_prog(struct nvkm_clk *base)
601
{
602
	struct gk20a_clk *clk = gk20a_clk(base);
603

B
Ben Skeggs 已提交
604
	return gk20a_pllg_program_mnp(clk);
605 606 607
}

static void
608
gk20a_clk_tidy(struct nvkm_clk *base)
609 610 611
{
}

612 613
static void
gk20a_clk_fini(struct nvkm_clk *base)
614
{
615
	struct nvkm_device *device = base->subdev.device;
616
	struct gk20a_clk *clk = gk20a_clk(base);
617 618 619 620 621
	u32 val;

	/* slide to VCO min */
	val = nvkm_rd32(device, GPCPLL_CFG);
	if (val & GPCPLL_CFG_ENABLE) {
622 623
		struct gk20a_pll pll;
		u32 n_lo;
624

625 626
		gk20a_pllg_read_mnp(clk, &pll);
		n_lo = DIV_ROUND_UP(pll.m * clk->params->min_vco,
627 628 629 630 631 632 633
				    clk->parent_rate / KHZ);
		gk20a_pllg_slide(clk, n_lo);
	}

	/* put PLL in bypass before disabling it */
	nvkm_mask(device, SEL_VCO, BIT(SEL_VCO_GPC2CLK_OUT_SHIFT), 0);

B
Ben Skeggs 已提交
634
	gk20a_pllg_disable(clk);
635 636 637
}

static int
638
gk20a_clk_init(struct nvkm_clk *base)
639
{
640
	struct gk20a_clk *clk = gk20a_clk(base);
641 642
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
643 644
	int ret;

645
	nvkm_mask(device, GPC2CLK_OUT, GPC2CLK_OUT_INIT_MASK, GPC2CLK_OUT_INIT_VAL);
646

B
Ben Skeggs 已提交
647
	ret = gk20a_clk_prog(&clk->base);
648
	if (ret) {
649
		nvkm_error(subdev, "cannot initialize clock\n");
650 651 652 653 654 655
		return ret;
	}

	return 0;
}

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
static const struct nvkm_clk_func
gk20a_clk = {
	.init = gk20a_clk_init,
	.fini = gk20a_clk_fini,
	.read = gk20a_clk_read,
	.calc = gk20a_clk_calc,
	.prog = gk20a_clk_prog,
	.tidy = gk20a_clk_tidy,
	.pstates = gk20a_pstates,
	.nr_pstates = ARRAY_SIZE(gk20a_pstates),
	.domains = {
		{ nv_clk_src_crystal, 0xff },
		{ nv_clk_src_gpc, 0xff, 0, "core", GK20A_CLK_GPC_MDIV },
		{ nv_clk_src_max }
	}
};

int
674 675 676 677
_gk20a_clk_ctor(struct nvkm_device *device, int index,
		const struct nvkm_clk_func *func,
		const struct gk20a_clk_pllg_params *params,
		struct gk20a_clk *clk)
678
{
679
	struct nvkm_device_tegra *tdev = device->func->tegra(device);
680 681
	int ret;
	int i;
682 683

	/* Finish initializing the pstates */
684 685 686
	for (i = 0; i < func->nr_pstates; i++) {
		INIT_LIST_HEAD(&func->pstates[i].list);
		func->pstates[i].pstate = i + 1;
687 688
	}

689
	clk->params = params;
690
	clk->parent_rate = clk_get_rate(tdev->clk);
691

692 693 694 695
	ret = nvkm_clk_ctor(func, device, index, true, &clk->base);
	if (ret)
		return ret;

696 697
	nvkm_debug(&clk->base.subdev, "parent clock rate: %d Khz\n",
		   clk->parent_rate / KHZ);
698

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	return 0;
}

int
gk20a_clk_new(struct nvkm_device *device, int index, struct nvkm_clk **pclk)
{
	struct gk20a_clk *clk;
	int ret;

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

	ret = _gk20a_clk_ctor(device, index, &gk20a_clk, &gk20a_pllg_params,
			      clk);

716 717 718
	clk->pl_to_div = pl_to_div;
	clk->div_to_pl = div_to_pl;

719
	return ret;
720
}