summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/clock_nvrm.c
blob: cc4dde9be1e6053571fc343ca65993d638cd0b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 * arch/arm/mach-tegra/tegra2_rm_clocks.c
 *
 * Clock controls layered on NvRm implementation
 *
 * Copyright (c) 2009-2010, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/err.h>
#include <linux/clk.h>

#include <asm/clkdev.h>
#include <asm/smp_twd.h>

#include <mach/iomap.h>
#include <mach/timex.h>

#include <mach/nvrm_linux.h>
#include "nvrm/core/common/nvrm_clocks.h"
#include "clock.h"
#include "nvrm_module.h"
#include "nvrm_power.h"

static LIST_HEAD(clocks);
static DEFINE_SPINLOCK(clock_lock);
static NvU32 clk_pwr_client;

struct clk *get_tegra_clock_by_name(const char *name)
{
	struct clk *c;
	struct clk *ret = NULL;
	unsigned long flags;
	spin_lock_irqsave(&clock_lock, flags);
	list_for_each_entry(c, &clocks, node) {
		if (strcmp(c->name, name) == 0) {
			ret = c;
			break;
		}
	}
	spin_unlock_irqrestore(&clock_lock, flags);
	return ret;
}

void tegra_periph_reset_deassert(struct clk *c)
{
	NvRmModuleResetWithHold(s_hRmGlobal, c->module, NV_FALSE);
}
EXPORT_SYMBOL(tegra_periph_reset_deassert);

void tegra_periph_reset_assert(struct clk *c)
{
	NvRmModuleResetWithHold(s_hRmGlobal, c->module, NV_TRUE);
}
EXPORT_SYMBOL(tegra_periph_reset_assert);

static void tegra_periph_clk_init(struct clk *c)
{
	NvRmModuleReset(s_hRmGlobal, c->module);
}

static int tegra_periph_clk_enable(struct clk *c)
{
	NvError e;

	if (c->power) {
		e = NvRmPowerVoltageControl(s_hRmGlobal, c->module,
			clk_pwr_client, NvRmVoltsUnspecified,
			NvRmVoltsUnspecified, NULL, 0, NULL);
		if (e!=NvSuccess) {
			pr_err("%s: failed to voltage control %s\n",
			       __func__, c->name);
			return -ENXIO;
		}
	}

	e = NvRmPowerModuleClockControl(s_hRmGlobal, c->module,
		clk_pwr_client, NV_TRUE);

	if (e!=NvSuccess) {
		pr_err("%s: failed to clock control %s\n", __func__, c->name);
		return -ENXIO;
	}

	/* max out emc when 3d is on */
	if (NVRM_MODULE_ID_MODULE(c->module) == NvRmModuleID_3D) {
		NvRmPowerBusyHint(s_hRmGlobal, NvRmDfsClockId_Emc, clk_pwr_client,
				0xffffffff, NvRmFreqMaximum);
	}

	return 0;
}

static void tegra_periph_clk_disable(struct clk *c)
{
	NvError e;

	if (NVRM_MODULE_ID_MODULE(c->module) == NvRmModuleID_3D) {
		NvRmPowerBusyHint(s_hRmGlobal, NvRmDfsClockId_Emc, clk_pwr_client, 0, 0);
	}

	e = NvRmPowerModuleClockControl(s_hRmGlobal, c->module,
		clk_pwr_client, NV_FALSE);

	if (e!=NvSuccess)
		pr_err("%s: failed to disable %s\n", __func__, c->name);

	if (c->power) {
		e = NvRmPowerVoltageControl(s_hRmGlobal, c->module,
		clk_pwr_client,	NvRmVoltsOff, NvRmVoltsOff, NULL, 0, NULL);

		if (e!=NvSuccess)
			pr_err("%s: failed to disable %s\n", __func__, c->name);
	}
}

static int tegra_periph_clk_set_rate(struct clk *c, unsigned long rate)
{
	NvError e;
	NvRmFreqKHz freq = rate / 1000;
	NvRmFreqKHz min, max;

	if (c->rate_tolerance) {
		NvRmFreqKHz temp = freq * c->rate_tolerance / 100;
		min = freq - temp;
		max = freq + temp;
	} else if (c->rate_min) {
		max = NvRmFreqMaximum;
		min = c->rate_min;
		freq = max_t(NvRmFreqKHz, c->rate_min, freq);
	} else {
		/* If no tolerance, and no low limit - let RM find the
		   best approximation to the target */
		max = min = NvRmFreqUnspecified;
	}

	e = NvRmPowerModuleClockConfig(s_hRmGlobal, c->module, clk_pwr_client,
		min, max, &freq, 1, &freq, 0);

	if (e!=NvSuccess) {
		pr_err("%s: failed to configure %s to %luHz\n",
			 __func__, c->name, rate);
		return -EIO;
	}

	pr_debug("%s: requested %luKHz, got %uKHz\n", c->name, rate/1000, freq);

	return 0;
}

static unsigned long tegra_periph_clk_get_rate(struct clk *c)
{
	NvError e;
	NvRmFreqKHz freq;

	e = NvRmPowerModuleClockConfig(s_hRmGlobal, c->module,
		clk_pwr_client, 0, 0, NULL, 0, &freq, 0);

	if (e != NvSuccess) {
		pr_debug("%s: failed to read %s\n", __func__, c->name);
		return 0;
	}

	return (unsigned long)freq * 1000;
}

static long tegra_periph_clk_round_rate(struct clk *c, unsigned long rate)
{
	NvRmFreqKHz max;
	/* Keep Host on low power PLLP */
	if (c->module == NvRmModuleID_GraphicsHost)
		max = NVRM_PLLP_FIXED_FREQ_KHZ / 2;
	else
		max = NvRmPowerModuleGetMaxFrequency(s_hRmGlobal, c->module);
	return min(((unsigned long)max) * 1000, rate);
}

static struct clk_ops tegra_periph_clk_ops = {
	.init = tegra_periph_clk_init,
	.enable = tegra_periph_clk_enable,
	.disable = tegra_periph_clk_disable,
	.set_rate = tegra_periph_clk_set_rate,
	.get_rate = tegra_periph_clk_get_rate,
	.round_rate = tegra_periph_clk_round_rate,
};

static unsigned long tegra_clksrc_clk_get_rate(struct clk *c)
{
	NvRmFreqKHz freq;

	freq = NvRmPrivGetClockSourceFreq(c->module);
	return freq * 1000;
}

static struct clk_ops clksrc_clk_ops = {
	.get_rate = tegra_clksrc_clk_get_rate,
};

static unsigned long tegra_dfs_clk_get_rate(struct clk *c)
{
	NvError e;
	NvRmDfsClockUsage usage;

	e = NvRmDfsGetClockUtilization(s_hRmGlobal, c->module, &usage);
	if (e != NvSuccess) {
		pr_debug("%s: failed to read %s\n", __func__, c->name);
		return 0;
	}
	return (unsigned long)usage.CurrentKHz * 1000;
}

static struct clk_ops dfs_clk_ops = {
	.get_rate = tegra_dfs_clk_get_rate,
};

#define NvRmModuleID_Pcie NvRmPrivModuleID_Pcie
#define NvRmModuleID_Afi NvRmPrivModuleID_Afi
#define NvRmModuleID_PcieXclk NvRmPrivModuleID_PcieXclk

#define PERIPH_CLK(_name, _dev, _con, _modname, _instance, _tol, _min, _pow) \
	{								\
		.name = _name,						\
		.lookup = {						\
			.dev_id = _dev,					\
			.con_id = _con,					\
		},							\
		.module = NVRM_MODULE_ID(NvRmModuleID_##_modname, _instance), \
		.ops = &tegra_periph_clk_ops,				\
		.rate_min = _min,					\
		.rate_tolerance = _tol,					\
		.power = _pow,						\
	}

static struct clk tegra_periph_clk[] = {
	PERIPH_CLK("rtc", "rtc-tegra", NULL, Rtc, 0, 0, 0, false),
	PERIPH_CLK("kbc", "tegra-kbc", NULL, Kbc, 0, 0, 0, false),
	PERIPH_CLK("uarta", "uart.0", NULL, Uart, 0, 5, 0, true),
	PERIPH_CLK("uartb", "uart.1", NULL, Uart, 1, 5, 0, true),
	PERIPH_CLK("uartc", "uart.2", NULL, Uart, 2, 5, 0, true),
	PERIPH_CLK("uartd", "uart.3", NULL, Uart, 3, 5, 0, true),
	PERIPH_CLK("uarte", "uart.4", NULL, Uart, 4, 5, 0, true),
	PERIPH_CLK("sdmmc1", "tegra-sdhci.0", NULL, Sdio, 0, 0, 400, false),
	PERIPH_CLK("sdmmc2", "tegra-sdhci.1", NULL, Sdio, 1, 0, 400, false),
	PERIPH_CLK("sdmmc3", "tegra-sdhci.2", NULL, Sdio, 2, 0, 400, false),
	PERIPH_CLK("sdmmc4", "tegra-sdhci.3", NULL, Sdio, 3, 0, 400, false),
	PERIPH_CLK("pcie", "tegra_pcie", NULL, Pcie, 0, 0, 0, true),
	PERIPH_CLK("pcie_xclk", "tegra_pcie_xclk", NULL, PcieXclk, 0, 0, 0, false),
	PERIPH_CLK("gr3d", "tegra_grhost", "gr3d", 3D, 0, 0, 0, true),
	PERIPH_CLK("gr2d", "tegra_grhost", "gr2d", 2D, 0, 0, 0, true),
	PERIPH_CLK("host1x", "tegra_grhost", "host1x", GraphicsHost, 0, 0, 0, true),
	PERIPH_CLK("epp", "tegra_grhost", "epp", Epp, 0, 0, 0, true),
};

static struct clk tegra_clk_cpu = {
	.name = "cpu",
	.module = NvRmDfsClockId_Cpu,
	.ops = &dfs_clk_ops,
};

static struct clk tegra_clk_pclk = {
	.name = "pclk",
	.module = NvRmDfsClockId_Apb,
	.ops = &dfs_clk_ops,
};

static struct clk tegra_clk_hclk = {
	.name = "hclk",
	.module = NvRmDfsClockId_Ahb,
	.ops = &dfs_clk_ops,
};

static struct clk tegra_clk_sys = {
	.name = "sys",
	.module = NvRmDfsClockId_System,
	.ops = &dfs_clk_ops,
};

static struct clk tegra_clk_pllp = {
	.name = "pll_p",
	.module = NvRmClockSource_PllP0,
	.ops = &clksrc_clk_ops,
};

#define DFS_CLK(dev, con, ck)			\
	{					\
		.dev_id = dev,			\
		.con_id = con,			\
		.clk = ck,			\
	}

static struct clk_lookup tegra_clk_lookups[] = {
	DFS_CLK(NULL, "cpu", &tegra_clk_cpu),
	DFS_CLK(NULL, "pclk", &tegra_clk_pclk),
	DFS_CLK(NULL, "hclk", &tegra_clk_hclk),
	DFS_CLK(NULL, "sys", &tegra_clk_sys),
	DFS_CLK(NULL, "pll_p", &tegra_clk_pllp),
};

#define CLK_DUPLICATE(_name, _dev, _con) \
	{						\
		.name      = _name,			\
		.lookup    = {				\
			.dev_id    = _dev,		\
			.con_id	   = _con,		\
		},					\
	}

/* Some clocks may be used by different drivers depending on the board
 * configuration.  List those here to register them twice in the clock lookup
 * table under two names.
 */
struct clk_duplicate tegra_clk_duplicates[] = {
	CLK_DUPLICATE("uarta", "tegra_uart.0", NULL),
	CLK_DUPLICATE("uartb", "tegra_uart.1", NULL),
	CLK_DUPLICATE("uartc", "tegra_uart.2", NULL),
	CLK_DUPLICATE("uartd", "tegra_uart.3", NULL),
	CLK_DUPLICATE("uarte", "tegra_uart.4", NULL),
};

void __init tegra2_init_clocks(void)
{
	int i;
	struct clk_lookup *cl;
	struct clk *c;
	struct clk_duplicate *cd;

	for (i=0; i<ARRAY_SIZE(tegra_clk_lookups); i++) {
		cl = &tegra_clk_lookups[i];
		clk_init(cl->clk);
		clkdev_add(cl);
	}

	for (i=0; i<ARRAY_SIZE(tegra_periph_clk); i++) {
		c = &tegra_periph_clk[i];
		cl = &c->lookup;
		cl->clk = c;
		clk_init(c);
		clkdev_add(cl);
	}

	for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
		cd = &tegra_clk_duplicates[i];
		c = get_tegra_clock_by_name(cd->name);
		if (c) {
			cl = &cd->lookup;
			cl->clk = c;
			clkdev_add(cl);
		} else {
			pr_err("%s: Unknown duplicate clock %s\n", __func__,
				cd->name);
		}
	}
}

void clk_init(struct clk *c)
{
	unsigned long flags;

	if (c->ops && c->ops->init)
		c->ops->init(c);

	spin_lock_irqsave(&clock_lock, flags);
	list_add(&c->node, &clocks);
	spin_unlock_irqrestore(&clock_lock, flags);
}

int clk_enable(struct clk *c)
{
	if (!c)
		return -ENODEV;

	if (c->ops && c->ops->enable)
		return c->ops->enable(c);

	return -ENOSYS;
}
EXPORT_SYMBOL(clk_enable);

void clk_disable(struct clk *c)
{
	if (c->ops && c->ops->disable)
		c->ops->disable(c);
}
EXPORT_SYMBOL(clk_disable);

int clk_set_rate(struct clk *c, unsigned long rate)
{
	if (c->ops && c->ops->set_rate)
		return c->ops->set_rate(c, rate);

	return -ENOSYS;
}
EXPORT_SYMBOL(clk_set_rate);

unsigned long clk_get_rate(struct clk *c)
{
	BUG_ON(!(c->ops && c->ops->get_rate));

	return c->ops->get_rate(c);
}
EXPORT_SYMBOL(clk_get_rate);

long clk_round_rate(struct clk *c, unsigned long rate)
{
	if (c->ops && c->ops->round_rate)
		return c->ops->round_rate(c, rate);

	return -ENOSYS;
}
EXPORT_SYMBOL(clk_round_rate);


void __init tegra_init_clock(void)
{
	NvError e;
	struct clk *cpu_clk = NULL;
	unsigned long rate = 0;

	e = NvRmOpenNew(&s_hRmGlobal);
	BUG_ON(e!=NvSuccess);

	NvRmPrivPostRegulatorInit(s_hRmGlobal);
	NvRmPowerRegister(s_hRmGlobal, 0, &clk_pwr_client);
	tegra2_init_clocks();

#ifdef CONFIG_USE_ARM_TWD_PRESCALER
	cpu_clk = clk_get_sys(NULL, "cpu");
	BUG_ON(IS_ERR(cpu_clk));

	rate = clk_get_rate(cpu_clk);
	local_timer_rescale(rate / 1000);
	clk_put(cpu_clk);
	on_each_cpu(twd_set_prescaler, NULL, true);
#endif
}

#ifdef CONFIG_PM
#define CLK_RESET_RST_DEVICES_L		0x04
#define CLK_RESET_RST_DEVICES_NUM	3

#define CLK_RESET_CLK_OUT_ENB_L		0x10
#define CLK_RESET_CLK_OUT_ENB_H		0x14
#define CLK_RESET_CLK_OUT_ENB_U		0x18
#define CLK_RESET_CLK_OUT_ENB_L_ALL	0xbffffff9ul
#define CLK_RESET_CLK_OUT_ENB_H_ALL	0xfefffff7ul
#define CLK_RESET_CLK_OUT_ENB_U_ALL	0x77f01bfful
#define CLK_RESET_CLK_OUT_ENB_NUM	3

#define CLK_RESET_CLK_MASK_ARM		0x44
#define CLK_RESET_MISC_CLK_ENB		0x48
#define CLK_RESET_OSC_CTRL		0x50
#define CLK_RESET_OSC_CTRL_MASK		0x3f2	/* drive strength & bypass */

#define CLK_RESET_CLK_SOURCE_I2S1	0x100
#define CLK_RESET_CLK_SOURCE_EMC	0x19c
#define CLK_RESET_CLK_SOURCE_OSC	0x1fc
#define CLK_RESET_CLK_SOURCE_NUM \
	(((CLK_RESET_CLK_SOURCE_OSC - CLK_RESET_CLK_SOURCE_I2S1) / 4) + 1 - 1)

static u32 clk_rst[CLK_RESET_RST_DEVICES_NUM + CLK_RESET_CLK_OUT_ENB_NUM +
		   CLK_RESET_CLK_SOURCE_NUM + 3];

void tegra_clk_suspend(void)
{
	void __iomem *car = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
	unsigned long offs, i;
	u32 *ctx = clk_rst;

	*ctx++ = readl(car + CLK_RESET_OSC_CTRL) & CLK_RESET_OSC_CTRL_MASK;

	for (offs=CLK_RESET_CLK_SOURCE_I2S1;
	     offs<=CLK_RESET_CLK_SOURCE_OSC; offs+=4) {

		if (offs==CLK_RESET_CLK_SOURCE_EMC)
			continue;

		*ctx++ = readl(car + offs);
	}

	offs = CLK_RESET_RST_DEVICES_L;
	for (i=0; i<CLK_RESET_RST_DEVICES_NUM; i++)
		*ctx++ = readl(car + offs + i*4);

	offs = CLK_RESET_CLK_OUT_ENB_L;
	for (i=0; i<CLK_RESET_CLK_OUT_ENB_NUM; i++)
		*ctx++ = readl(car + offs + i*4);

	*ctx++ = readl(car + CLK_RESET_MISC_CLK_ENB);
	*ctx++ = readl(car + CLK_RESET_CLK_MASK_ARM);

	BUG_ON(ctx-clk_rst != ARRAY_SIZE(clk_rst));
}

void tegra_clk_resume(void)
{
	void __iomem *car = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
	unsigned long offs, i;
	u32 *ctx = clk_rst;
	u32 temp;

	temp = readl(car + CLK_RESET_OSC_CTRL) & ~CLK_RESET_OSC_CTRL_MASK;
	temp |= *ctx++;
	writel(temp, car + CLK_RESET_OSC_CTRL);

	writel(CLK_RESET_CLK_OUT_ENB_L_ALL, car + CLK_RESET_CLK_OUT_ENB_L);
	writel(CLK_RESET_CLK_OUT_ENB_H_ALL, car + CLK_RESET_CLK_OUT_ENB_H);
	writel(CLK_RESET_CLK_OUT_ENB_U_ALL, car + CLK_RESET_CLK_OUT_ENB_U);
	wmb();

	for (offs=CLK_RESET_CLK_SOURCE_I2S1;
	     offs<=CLK_RESET_CLK_SOURCE_OSC; offs+=4) {
		if (offs==CLK_RESET_CLK_SOURCE_EMC)
			continue;
		writel(*ctx++, car + offs);
	}
	wmb();

	offs = CLK_RESET_RST_DEVICES_L;
	for (i=0; i<CLK_RESET_RST_DEVICES_NUM; i++)
		writel(*ctx++, car + offs + i*4);
	wmb();

	offs = CLK_RESET_CLK_OUT_ENB_L;
	for (i=0; i<CLK_RESET_CLK_OUT_ENB_NUM; i++)
		writel(*ctx++, car + offs + i*4);
	wmb();

	writel(*ctx++, car + CLK_RESET_MISC_CLK_ENB);
	writel(*ctx++, car + CLK_RESET_CLK_MASK_ARM);
	BUG_ON(ctx-clk_rst != ARRAY_SIZE(clk_rst));
}

#endif