summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-imx/speed-imx35.c
blob: a4e0120da5cbec22c4aaf82889c5b4785c7ba7ee (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
/*
 * 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.
 *
 */

#include <common.h>
#include <asm-generic/errno.h>
#include <mach/imx-regs.h>
#include <io.h>
#include <mach/clock.h>
#include <mach/generic.h>
#include <init.h>

unsigned long imx_get_mpllclk(void)
{
	ulong mpctl = readl(IMX_CCM_BASE + CCM_MPCTL);
	return imx_decode_pll(mpctl, CONFIG_MX35_HCLK_FREQ);
}

static unsigned long imx_get_ppllclk(void)
{
	ulong ppctl = readl(IMX_CCM_BASE + CCM_PPCTL);
	return imx_decode_pll(ppctl, CONFIG_MX35_HCLK_FREQ);
}

struct arm_ahb_div {
	unsigned char arm, ahb, sel;
};

static struct arm_ahb_div clk_consumer[] = {
	{ .arm = 1, .ahb = 4, .sel = 0},
	{ .arm = 1, .ahb = 3, .sel = 1},
	{ .arm = 2, .ahb = 2, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
	{ .arm = 4, .ahb = 1, .sel = 0},
	{ .arm = 1, .ahb = 5, .sel = 0},
	{ .arm = 1, .ahb = 8, .sel = 0},
	{ .arm = 1, .ahb = 6, .sel = 1},
	{ .arm = 2, .ahb = 4, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
	{ .arm = 4, .ahb = 2, .sel = 0},
	{ .arm = 0, .ahb = 0, .sel = 0},
};

static unsigned long imx_get_armclk(void)
{
	unsigned long pdr0 = readl(IMX_CCM_BASE + CCM_PDR0);
	struct arm_ahb_div *aad;
	unsigned long fref = imx_get_mpllclk();

	/* consumer path is selected */
	aad = &clk_consumer[(pdr0 >> 16) & 0xf];
	if (aad->sel)
		fref = fref * 3 / 4;

	return fref / aad->arm;
}

unsigned long imx_get_ahbclk(void)
{
	unsigned long pdr0 = readl(IMX_CCM_BASE + CCM_PDR0);
	struct arm_ahb_div *aad;
	unsigned long fref = imx_get_mpllclk();

	aad = &clk_consumer[(pdr0 >> 16) & 0xf];
	if (aad->sel)
		fref = fref * 3 / 4;

	return fref / aad->ahb;
}

unsigned long imx_get_ipgclk(void)
{
	ulong clk = imx_get_ahbclk();

	return clk >> 1;
}

static unsigned long get_3_3_div(unsigned long in)
{
	return (((in >> 3) & 0x7) + 1) * ((in & 0x7) + 1);
}

static unsigned long get_6_div(unsigned long in)
{
	return ((in & 0x3f) + 1);
}

static unsigned long imx_get_ipg_perclk(void)
{
	ulong pdr0 = readl(IMX_CCM_BASE + CCM_PDR0);
	ulong pdr4 = readl(IMX_CCM_BASE + CCM_PDR4);
	ulong div;
	ulong fref;

	if (pdr0 & PDR0_PER_SEL) {
		/* perclk from arm high frequency clock and synched with AHB clki */
		fref = imx_get_armclk();
		div = get_3_3_div((pdr4 >> 16));
	} else {
		/* perclk from AHB divided clock */
		fref = imx_get_ahbclk();
		div = ((pdr0 >> 12) & 0x7) + 1;
	}

	return fref / div;
}

unsigned long imx_get_gptclk(void)
{
	return imx_get_ipgclk();
}

/**
 * Calculate the current pixel clock speed (aka HSP or IPU)
 * @return 0 on failure or current frequency in Hz
 */
unsigned long imx_get_lcdclk(void)
{
	unsigned long hsp_podf = (readl(IMX_CCM_BASE + CCM_PDR0) >> 20) & 0x03;
	unsigned long base_clk = imx_get_armclk();

	if (base_clk > 400 * 1000 * 1000) {
		switch(hsp_podf) {
		case 0:
			return base_clk >> 2;
		case 1:
			return base_clk >> 3;
		case 2:
			return base_clk / 3;
		}
	} else {
		switch(hsp_podf) {
		case 0:
		case 2:
			return base_clk / 3;
		case 1:
			return base_clk / 6;
		}
	}

	return 0;
}

unsigned long imx_get_uartclk(void)
{
	unsigned long pdr3 = readl(IMX_CCM_BASE + CCM_PDR3);
	unsigned long pdr4 = readl(IMX_CCM_BASE + CCM_PDR4);
	unsigned long div = get_3_3_div(pdr4 >> 10);

	if (pdr3 & (1 << 14))
		return imx_get_armclk() / div;
	else
		return imx_get_ppllclk() / div;
}

/* mmc0 clk only */
unsigned long imx_get_mmcclk(void)
{
	unsigned long pdr3 = readl(IMX_CCM_BASE + CCM_PDR3);
	unsigned long div = get_6_div(pdr3);

	if (pdr3 & (1 << 6))
		return imx_get_armclk() / div;
	else
		return imx_get_ppllclk() / div;
}

ulong imx_get_fecclk(void)
{
	return imx_get_ipgclk();
}

ulong fsl_get_i2cclk(void)
{
	return imx_get_ipg_perclk();
}

unsigned long imx_get_cspiclk(void)
{
	return imx_get_ipgclk();
}

void imx_dump_clocks(void)
{
	printf("mpll:    %10ld Hz\n", imx_get_mpllclk());
	printf("ppll:    %10ld Hz\n", imx_get_ppllclk());
	printf("arm:     %10ld Hz\n", imx_get_armclk());
	printf("gpt:     %10ld Hz\n", imx_get_gptclk());
	printf("ahb:     %10ld Hz\n", imx_get_ahbclk());
	printf("ipg:     %10ld Hz\n", imx_get_ipgclk());
	printf("ipg_per: %10ld Hz\n", imx_get_ipg_perclk());
	printf("uart:	 %10ld Hz\n", imx_get_uartclk());
	printf("sdhc1:   %10ld Hz\n", imx_get_mmcclk());
}

/*
 * Set the divider of the CLKO pin. Returns
 * the new divider (which may be smaller
 * than the desired one)
 */
int imx_clko_set_div(int num, int div)
{
	unsigned long cosr = readl(IMX_CCM_BASE + CCM_COSR);

	if (num != 1)
		return -ENODEV;

	div -= 1;
	div &= 0x3f;

	cosr &= ~(0x3f << 10);
	cosr |= div << 10;

	writel(cosr, IMX_CCM_BASE + CCM_COSR);

	return div + 1;
}

/*
 * Set the clock source for the CLKO pin
 */
void imx_clko_set_src(int num, int src)
{
	unsigned long cosr = readl(IMX_CCM_BASE + CCM_COSR);

	if (num != 1)
		return;

	if (src < 0) {
		cosr &= ~(1 << 5);
		writel(cosr, IMX_CCM_BASE + CCM_COSR);
		return;
	}

	cosr |= 1 << 5;
	cosr &= ~0x1f;
	cosr &= ~(1 << 6);
	cosr |= src & 0x1f;

	writel(cosr, IMX_CCM_BASE + CCM_COSR);
}