summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/clock-common.c
blob: 67c5d51378ae6269616d420d8f13ac047cb8cf06 (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
/*
 * Copyright (C) 2010-2012 NVIDIA Corporation
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/kernel.h>
#include <linux/clk.h>
#include <linux/clkdev.h>
#include <linux/io.h>

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

#include "clock.h"

#define clk_writel(value, reg) \
	__raw_writel(value, (u32)reg_clk_base + (reg))
#define clk_readl(reg) \
	__raw_readl((u32)reg_clk_base + (reg))

#define OSC_FREQ_DET			0x58
#define OSC_FREQ_DET_TRIG		(1<<31)

#define OSC_FREQ_DET_STATUS		0x5C
#define OSC_FREQ_DET_BUSY		(1<<31)
#define OSC_FREQ_DET_CNT_MASK		0xFFFF

static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
static unsigned long osc_input_freq;

unsigned long tegra_clk_measure_input_freq(void)
{
	u32 clock_autodetect;

	if (osc_input_freq)
		return osc_input_freq;

	clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
	do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
	clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);

	if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3)
		osc_input_freq = 12000000;
	else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3)
		osc_input_freq = 13000000;
	else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3)
		osc_input_freq = 19200000;
	else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3)
		osc_input_freq = 26000000;
#ifndef CONFIG_ARCH_TEGRA_2x_SOC
	else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3)
		osc_input_freq = 16800000;
	else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3)
		osc_input_freq = 38400000;
	else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3)
		osc_input_freq = 48000000;
#endif
	else {
		pr_err("%s: Unexpected clock autodetect value %d", __func__,
			clock_autodetect);
		BUG();
	}
	return osc_input_freq;
}