gk20a.c 16.6 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 24
/*
 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
 *
 * 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 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#define MHZ (1000 * 1000)

#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)

static const u8 pl_to_div[] = {
/* 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,
};

/* All frequencies in Mhz */
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;
};

static const struct gk20a_clk_pllg_params gk20a_pllg_params = {
110
	.min_vco = 1000, .max_vco = 2064,
111 112 113 114 115 116
	.min_u = 12, .max_u = 38,
	.min_m = 1, .max_m = 255,
	.min_n = 8, .max_n = 255,
	.min_pl = 1, .max_pl = 32,
};

B
Ben Skeggs 已提交
117
struct gk20a_clk {
118
	struct nvkm_clk base;
119 120 121 122 123 124
	const struct gk20a_clk_pllg_params *params;
	u32 m, n, pl;
	u32 parent_rate;
};

static void
B
Ben Skeggs 已提交
125
gk20a_pllg_read_mnp(struct gk20a_clk *clk)
126
{
127
	struct nvkm_device *device = clk->base.subdev.device;
128 129
	u32 val;

130
	val = nvkm_rd32(device, GPCPLL_COEFF);
B
Ben Skeggs 已提交
131 132 133
	clk->m = (val >> GPCPLL_COEFF_M_SHIFT) & MASK(GPCPLL_COEFF_M_WIDTH);
	clk->n = (val >> GPCPLL_COEFF_N_SHIFT) & MASK(GPCPLL_COEFF_N_WIDTH);
	clk->pl = (val >> GPCPLL_COEFF_P_SHIFT) & MASK(GPCPLL_COEFF_P_WIDTH);
134 135 136
}

static u32
B
Ben Skeggs 已提交
137
gk20a_pllg_calc_rate(struct gk20a_clk *clk)
138 139 140 141
{
	u32 rate;
	u32 divider;

B
Ben Skeggs 已提交
142 143
	rate = clk->parent_rate * clk->n;
	divider = clk->m * pl_to_div[clk->pl];
144

145
	return rate / divider / 2;
146 147 148
}

static int
B
Ben Skeggs 已提交
149
gk20a_pllg_calc_mnp(struct gk20a_clk *clk, unsigned long rate)
150
{
151
	struct nvkm_subdev *subdev = &clk->base.subdev;
152 153 154 155 156 157 158 159 160 161 162
	u32 target_clk_f, ref_clk_f, target_freq;
	u32 min_vco_f, max_vco_f;
	u32 low_pl, high_pl, best_pl;
	u32 target_vco_f, vco_f;
	u32 best_m, best_n;
	u32 u_f;
	u32 m, n, n2;
	u32 delta, lwv, best_delta = ~0;
	u32 pl;

	target_clk_f = rate * 2 / MHZ;
B
Ben Skeggs 已提交
163
	ref_clk_f = clk->parent_rate / MHZ;
164

B
Ben Skeggs 已提交
165 166 167 168 169
	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;
170 171 172 173 174 175 176

	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 已提交
177 178
	high_pl = min(high_pl, clk->params->max_pl);
	high_pl = max(high_pl, clk->params->min_pl);
179 180 181

	/* min_pl <= low_pl <= max_pl */
	low_pl = min_vco_f / target_vco_f;
B
Ben Skeggs 已提交
182 183
	low_pl = min(low_pl, clk->params->max_pl);
	low_pl = max(low_pl, clk->params->min_pl);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

	/* Find Indices of high_pl and low_pl */
	for (pl = 0; pl < ARRAY_SIZE(pl_to_div) - 1; pl++) {
		if (pl_to_div[pl] >= low_pl) {
			low_pl = pl;
			break;
		}
	}
	for (pl = 0; pl < ARRAY_SIZE(pl_to_div) - 1; pl++) {
		if (pl_to_div[pl] >= high_pl) {
			high_pl = pl;
			break;
		}
	}

199 200
	nvkm_debug(subdev, "low_PL %d(div%d), high_PL %d(div%d)", low_pl,
		   pl_to_div[low_pl], high_pl, pl_to_div[high_pl]);
201 202 203 204

	/* Select lowest possible VCO */
	for (pl = low_pl; pl <= high_pl; pl++) {
		target_vco_f = target_clk_f * pl_to_div[pl];
B
Ben Skeggs 已提交
205
		for (m = clk->params->min_m; m <= clk->params->max_m; m++) {
206 207
			u_f = ref_clk_f / m;

B
Ben Skeggs 已提交
208
			if (u_f < clk->params->min_u)
209
				break;
B
Ben Skeggs 已提交
210
			if (u_f > clk->params->max_u)
211 212 213 214 215
				continue;

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

B
Ben Skeggs 已提交
216
			if (n > clk->params->max_n)
217 218 219
				break;

			for (; n <= n2; n++) {
B
Ben Skeggs 已提交
220
				if (n < clk->params->min_n)
221
					continue;
B
Ben Skeggs 已提交
222
				if (n > clk->params->max_n)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
					break;

				vco_f = ref_clk_f * n / m;

				if (vco_f >= min_vco_f && vco_f <= max_vco_f) {
					lwv = (vco_f + (pl_to_div[pl] / 2))
						/ pl_to_div[pl];
					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)
250 251 252
		nvkm_debug(subdev,
			   "no best match for target @ %dMHz on gpc_pll",
			   target_clk_f);
253

B
Ben Skeggs 已提交
254 255 256
	clk->m = best_m;
	clk->n = best_n;
	clk->pl = best_pl;
257

B
Ben Skeggs 已提交
258
	target_freq = gk20a_pllg_calc_rate(clk) / MHZ;
259

260 261 262
	nvkm_debug(subdev,
		   "actual target freq %d MHz, M %d, N %d, PL %d(div%d)\n",
		   target_freq, clk->m, clk->n, clk->pl, pl_to_div[clk->pl]);
263 264 265 266
	return 0;
}

static int
B
Ben Skeggs 已提交
267
gk20a_pllg_slide(struct gk20a_clk *clk, u32 n)
268
{
269 270
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
271 272 273 274
	u32 val;
	int ramp_timeout;

	/* get old coefficients */
275
	val = nvkm_rd32(device, GPCPLL_COEFF);
276 277 278 279 280
	/* do nothing if NDIV is the same */
	if (n == ((val >> GPCPLL_COEFF_N_SHIFT) & MASK(GPCPLL_COEFF_N_WIDTH)))
		return 0;

	/* setup */
281
	nvkm_mask(device, GPCPLL_CFG2, 0xff << GPCPLL_CFG2_PLL_STEPA_SHIFT,
282
		0x2b << GPCPLL_CFG2_PLL_STEPA_SHIFT);
283
	nvkm_mask(device, GPCPLL_CFG3, 0xff << GPCPLL_CFG3_PLL_STEPB_SHIFT,
284 285 286
		0xb << GPCPLL_CFG3_PLL_STEPB_SHIFT);

	/* pll slowdown mode */
287
	nvkm_mask(device, GPCPLL_NDIV_SLOWDOWN,
288 289 290 291
		BIT(GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT),
		BIT(GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT));

	/* new ndiv ready for ramp */
292
	val = nvkm_rd32(device, GPCPLL_COEFF);
293 294 295
	val &= ~(MASK(GPCPLL_COEFF_N_WIDTH) << GPCPLL_COEFF_N_SHIFT);
	val |= (n & MASK(GPCPLL_COEFF_N_WIDTH)) << GPCPLL_COEFF_N_SHIFT;
	udelay(1);
296
	nvkm_wr32(device, GPCPLL_COEFF, val);
297 298

	/* dynamic ramp to new ndiv */
299
	val = nvkm_rd32(device, GPCPLL_NDIV_SLOWDOWN);
300 301
	val |= 0x1 << GPCPLL_NDIV_SLOWDOWN_EN_DYNRAMP_SHIFT;
	udelay(1);
302
	nvkm_wr32(device, GPCPLL_NDIV_SLOWDOWN, val);
303 304 305

	for (ramp_timeout = 500; ramp_timeout > 0; ramp_timeout--) {
		udelay(1);
306
		val = nvkm_rd32(device, GPC_BCAST_NDIV_SLOWDOWN_DEBUG);
307 308 309 310 311
		if (val & GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_MASK)
			break;
	}

	/* exit slowdown mode */
312
	nvkm_mask(device, GPCPLL_NDIV_SLOWDOWN,
313 314
		BIT(GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT) |
		BIT(GPCPLL_NDIV_SLOWDOWN_EN_DYNRAMP_SHIFT), 0);
315
	nvkm_rd32(device, GPCPLL_NDIV_SLOWDOWN);
316 317

	if (ramp_timeout <= 0) {
318
		nvkm_error(subdev, "gpcpll dynamic ramp timeout\n");
319 320 321 322 323 324 325
		return -ETIMEDOUT;
	}

	return 0;
}

static void
B
Ben Skeggs 已提交
326
_gk20a_pllg_enable(struct gk20a_clk *clk)
327
{
328 329 330
	struct nvkm_device *device = clk->base.subdev.device;
	nvkm_mask(device, GPCPLL_CFG, GPCPLL_CFG_ENABLE, GPCPLL_CFG_ENABLE);
	nvkm_rd32(device, GPCPLL_CFG);
331 332 333
}

static void
B
Ben Skeggs 已提交
334
_gk20a_pllg_disable(struct gk20a_clk *clk)
335
{
336 337 338
	struct nvkm_device *device = clk->base.subdev.device;
	nvkm_mask(device, GPCPLL_CFG, GPCPLL_CFG_ENABLE, 0);
	nvkm_rd32(device, GPCPLL_CFG);
339 340 341
}

static int
B
Ben Skeggs 已提交
342
_gk20a_pllg_program_mnp(struct gk20a_clk *clk, bool allow_slide)
343
{
344 345
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
346 347 348 349
	u32 val, cfg;
	u32 m_old, pl_old, n_lo;

	/* get old coefficients */
350
	val = nvkm_rd32(device, GPCPLL_COEFF);
351 352 353 354
	m_old = (val >> GPCPLL_COEFF_M_SHIFT) & MASK(GPCPLL_COEFF_M_WIDTH);
	pl_old = (val >> GPCPLL_COEFF_P_SHIFT) & MASK(GPCPLL_COEFF_P_WIDTH);

	/* do NDIV slide if there is no change in M and PL */
355
	cfg = nvkm_rd32(device, GPCPLL_CFG);
B
Ben Skeggs 已提交
356
	if (allow_slide && clk->m == m_old && clk->pl == pl_old &&
357
	    (cfg & GPCPLL_CFG_ENABLE)) {
B
Ben Skeggs 已提交
358
		return gk20a_pllg_slide(clk, clk->n);
359 360 361
	}

	/* slide down to NDIV_LO */
B
Ben Skeggs 已提交
362 363
	n_lo = DIV_ROUND_UP(m_old * clk->params->min_vco,
			    clk->parent_rate / MHZ);
364
	if (allow_slide && (cfg & GPCPLL_CFG_ENABLE)) {
B
Ben Skeggs 已提交
365
		int ret = gk20a_pllg_slide(clk, n_lo);
366 367 368 369 370 371

		if (ret)
			return ret;
	}

	/* split FO-to-bypass jump in halfs by setting out divider 1:2 */
372
	nvkm_mask(device, GPC2CLK_OUT, GPC2CLK_OUT_VCODIV_MASK,
373 374 375
		0x2 << GPC2CLK_OUT_VCODIV_SHIFT);

	/* put PLL in bypass before programming it */
376
	val = nvkm_rd32(device, SEL_VCO);
377 378
	val &= ~(BIT(SEL_VCO_GPC2CLK_OUT_SHIFT));
	udelay(2);
379
	nvkm_wr32(device, SEL_VCO, val);
380 381

	/* get out from IDDQ */
382
	val = nvkm_rd32(device, GPCPLL_CFG);
383 384
	if (val & GPCPLL_CFG_IDDQ) {
		val &= ~GPCPLL_CFG_IDDQ;
385 386
		nvkm_wr32(device, GPCPLL_CFG, val);
		nvkm_rd32(device, GPCPLL_CFG);
387 388 389
		udelay(2);
	}

B
Ben Skeggs 已提交
390
	_gk20a_pllg_disable(clk);
391

392 393
	nvkm_debug(subdev, "%s: m=%d n=%d pl=%d\n", __func__,
		   clk->m, clk->n, clk->pl);
394

B
Ben Skeggs 已提交
395 396 397 398 399
	n_lo = DIV_ROUND_UP(clk->m * clk->params->min_vco,
			    clk->parent_rate / MHZ);
	val = clk->m << GPCPLL_COEFF_M_SHIFT;
	val |= (allow_slide ? n_lo : clk->n) << GPCPLL_COEFF_N_SHIFT;
	val |= clk->pl << GPCPLL_COEFF_P_SHIFT;
400
	nvkm_wr32(device, GPCPLL_COEFF, val);
401

B
Ben Skeggs 已提交
402
	_gk20a_pllg_enable(clk);
403

404
	val = nvkm_rd32(device, GPCPLL_CFG);
405 406
	if (val & GPCPLL_CFG_LOCK_DET_OFF) {
		val &= ~GPCPLL_CFG_LOCK_DET_OFF;
407
		nvkm_wr32(device, GPCPLL_CFG, val);
408 409
	}

410 411 412 413
	if (nvkm_usec(device, 300,
		if (nvkm_rd32(device, GPCPLL_CFG) & GPCPLL_CFG_LOCK)
			break;
	) < 0)
414 415 416
		return -ETIMEDOUT;

	/* switch to VCO mode */
417
	nvkm_mask(device, SEL_VCO, 0, BIT(SEL_VCO_GPC2CLK_OUT_SHIFT));
418 419

	/* restore out divider 1:1 */
420
	val = nvkm_rd32(device, GPC2CLK_OUT);
421 422
	val &= ~GPC2CLK_OUT_VCODIV_MASK;
	udelay(2);
423
	nvkm_wr32(device, GPC2CLK_OUT, val);
424 425

	/* slide up to new NDIV */
B
Ben Skeggs 已提交
426
	return allow_slide ? gk20a_pllg_slide(clk, clk->n) : 0;
427 428 429
}

static int
B
Ben Skeggs 已提交
430
gk20a_pllg_program_mnp(struct gk20a_clk *clk)
431 432 433
{
	int err;

B
Ben Skeggs 已提交
434
	err = _gk20a_pllg_program_mnp(clk, true);
435
	if (err)
B
Ben Skeggs 已提交
436
		err = _gk20a_pllg_program_mnp(clk, false);
437 438 439 440 441

	return err;
}

static void
B
Ben Skeggs 已提交
442
gk20a_pllg_disable(struct gk20a_clk *clk)
443
{
444
	struct nvkm_device *device = clk->base.subdev.device;
445 446 447
	u32 val;

	/* slide to VCO min */
448
	val = nvkm_rd32(device, GPCPLL_CFG);
449 450 451
	if (val & GPCPLL_CFG_ENABLE) {
		u32 coeff, m, n_lo;

452
		coeff = nvkm_rd32(device, GPCPLL_COEFF);
453
		m = (coeff >> GPCPLL_COEFF_M_SHIFT) & MASK(GPCPLL_COEFF_M_WIDTH);
B
Ben Skeggs 已提交
454 455 456
		n_lo = DIV_ROUND_UP(m * clk->params->min_vco,
				    clk->parent_rate / MHZ);
		gk20a_pllg_slide(clk, n_lo);
457 458 459
	}

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

B
Ben Skeggs 已提交
462
	_gk20a_pllg_disable(clk);
463 464 465 466
}

#define GK20A_CLK_GPC_MDIV 1000

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

static int
562
gk20a_clk_read(struct nvkm_clk *base, enum nv_clk_src src)
563
{
564
	struct gk20a_clk *clk = gk20a_clk(base);
565 566
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
567 568 569

	switch (src) {
	case nv_clk_src_crystal:
570
		return device->crystal;
571
	case nv_clk_src_gpc:
B
Ben Skeggs 已提交
572 573
		gk20a_pllg_read_mnp(clk);
		return gk20a_pllg_calc_rate(clk) / GK20A_CLK_GPC_MDIV;
574
	default:
575
		nvkm_error(subdev, "invalid clock source %d\n", src);
576 577 578 579 580
		return -EINVAL;
	}
}

static int
581
gk20a_clk_calc(struct nvkm_clk *base, struct nvkm_cstate *cstate)
582
{
583
	struct gk20a_clk *clk = gk20a_clk(base);
584

B
Ben Skeggs 已提交
585
	return gk20a_pllg_calc_mnp(clk, cstate->domain[nv_clk_src_gpc] *
586 587 588 589
					 GK20A_CLK_GPC_MDIV);
}

static int
590
gk20a_clk_prog(struct nvkm_clk *base)
591
{
592
	struct gk20a_clk *clk = gk20a_clk(base);
593

B
Ben Skeggs 已提交
594
	return gk20a_pllg_program_mnp(clk);
595 596 597
}

static void
598
gk20a_clk_tidy(struct nvkm_clk *base)
599 600 601
{
}

602 603
static void
gk20a_clk_fini(struct nvkm_clk *base)
604
{
605
	struct gk20a_clk *clk = gk20a_clk(base);
B
Ben Skeggs 已提交
606
	gk20a_pllg_disable(clk);
607 608 609
}

static int
610
gk20a_clk_init(struct nvkm_clk *base)
611
{
612
	struct gk20a_clk *clk = gk20a_clk(base);
613 614
	struct nvkm_subdev *subdev = &clk->base.subdev;
	struct nvkm_device *device = subdev->device;
615 616
	int ret;

617
	nvkm_mask(device, GPC2CLK_OUT, GPC2CLK_OUT_INIT_MASK, GPC2CLK_OUT_INIT_VAL);
618

B
Ben Skeggs 已提交
619
	ret = gk20a_clk_prog(&clk->base);
620
	if (ret) {
621
		nvkm_error(subdev, "cannot initialize clock\n");
622 623 624 625 626 627
		return ret;
	}

	return 0;
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
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
gk20a_clk_new(struct nvkm_device *device, int index, struct nvkm_clk **pclk)
647
{
648
	struct nvkm_device_tegra *tdev = device->func->tegra(device);
B
Ben Skeggs 已提交
649
	struct gk20a_clk *clk;
650 651 652 653 654
	int ret, i;

	if (!(clk = kzalloc(sizeof(*clk), GFP_KERNEL)))
		return -ENOMEM;
	*pclk = &clk->base;
655 656 657 658 659 660 661

	/* Finish initializing the pstates */
	for (i = 0; i < ARRAY_SIZE(gk20a_pstates); i++) {
		INIT_LIST_HEAD(&gk20a_pstates[i].list);
		gk20a_pstates[i].pstate = i + 1;
	}

B
Ben Skeggs 已提交
662
	clk->params = &gk20a_pllg_params;
663
	clk->parent_rate = clk_get_rate(tdev->clk);
664 665

	ret = nvkm_clk_ctor(&gk20a_clk, device, index, true, &clk->base);
666 667
	nvkm_info(&clk->base.subdev, "parent clock rate: %d Mhz\n",
		  clk->parent_rate / MHZ);
668
	return ret;
669
}