summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-tegra/tegra_avp_init.c
blob: 91fd894dca76194f8075a1b442ec18ac21610c98 (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
/*
 * Copyright (C) 2013-2014 Lucas Stach <l.stach@pengutronix.de>
 *
 * Partly based on code (C) Copyright 2010-2011
 * NVIDIA Corporation <www.nvidia.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <asm/barebox-arm-head.h>
#include <asm/barebox-arm.h>
#include <mach/lowlevel.h>
#include <mach/tegra20-car.h>
#include <mach/tegra20-pmc.h>
#include <mach/tegra30-car.h>
#include <mach/tegra30-flow.h>
#include <mach/tegra124-car.h>

/* instruct the PMIC to enable the CPU power rail */
static void enable_maincomplex_powerrail(void)
{
	u32 reg;

	reg = readl(TEGRA_PMC_BASE + PMC_CNTRL);
	reg |= PMC_CNTRL_CPUPWRREQ_OE;
	writel(reg, TEGRA_PMC_BASE);
}

/* put every core in the main CPU complex into reset state */
static void assert_maincomplex_reset(int num_cores)
{
	u32 mask = 0;
	int i;

	for (i = 0; i < num_cores; i++) {
		if (tegra_get_chiptype() >= TEGRA114)
			mask |= 0x111001 << i;
		else
			mask |= 0x1111 << i;
	}

	writel(mask, TEGRA_CLK_RESET_BASE + CRC_RST_CPU_CMPLX_SET);
	writel(CRC_RST_DEV_L_CPU, TEGRA_CLK_RESET_BASE + CRC_RST_DEV_L_SET);
}

/* release reset state of the first core of the main CPU complex */
static void deassert_cpu0_reset(void)
{
	u32 reg;

	if (tegra_get_chiptype() >= TEGRA114)
		reg = 0x21fff00f;
	else
		reg = 0x1111;

	writel(reg, TEGRA_CLK_RESET_BASE + CRC_RST_CPU_CMPLX_CLR);
	writel(CRC_RST_DEV_L_CPU, TEGRA_CLK_RESET_BASE + CRC_RST_DEV_L_CLR);
}

/* stop all internal and external clocks to the main CPU complex */
static void stop_maincomplex_clocks(int num_cores)
{
	u32 reg;
	int i;

	reg = readl(TEGRA_CLK_RESET_BASE + CRC_CLK_CPU_CMPLX);
	for (i = 0; i < num_cores; i++)
		reg |= 0x1 << (8 + i);
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_CLK_CPU_CMPLX);

	reg = readl(TEGRA_CLK_RESET_BASE + CRC_CLK_OUT_ENB_L);
	reg &= ~CRC_CLK_OUT_ENB_L_CPU;
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_CLK_OUT_ENB_L);
}

struct pll_config {
	u16 divn;
	u16 divm;
	u16 divp;
	u16 cpcon;
};

static struct pll_config pllx_config_table[][4] = {
	{
		{1000, 13, 0, 12},	/* OSC 13.0 MHz */
		{625,  12, 0, 8 },	/* OSC 19.2 MHz */
		{1000, 12, 0, 12},	/* OSC 12.0 MHz */
		{1000, 26, 0, 12},	/* OSC 26.0 MHz */
	}, /* TEGRA 20 */
	{
		{600,  13, 0, 8 },
		{500,  16, 0, 8 },
		{600,  12, 0, 8 },
		{600,  26, 0, 8 },
	}, /* TEGRA 30 */
	{
	}, /* TEGRA 114 */
	{
		{ 108,  1, 1, 0 },
		{  73,  1, 1, 0 },
		{ 116,  1, 1, 0 },
		{ 108,  2, 1, 0 },
	}, /* TEGRA 124 */
};

static void init_pllx(void)
{
	struct pll_config *conf;
	enum tegra_chiptype chiptype;
	u8 osc_freq;
	u32 reg;

	/* If PLLX is already enabled, just return */
	if (readl(TEGRA_CLK_RESET_BASE + CRC_PLLX_BASE) & CRC_PLLX_BASE_ENABLE)
		return;

	chiptype = tegra_get_chiptype();

	/* disable IDDQ on T124 */
	if (chiptype == TEGRA124) {
		reg = readl(TEGRA_CLK_RESET_BASE + CRC_PLLX_MISC_3);
		reg &= ~CRC_PLLX_MISC_3_IDDQ;
		writel(reg, TEGRA_CLK_RESET_BASE + CRC_PLLX_MISC_3);
		tegra_ll_delay_usec(2);
	}

	osc_freq = (readl(TEGRA_CLK_RESET_BASE + CRC_OSC_CTRL) &
		    CRC_OSC_CTRL_OSC_FREQ_MASK) >> CRC_OSC_CTRL_OSC_FREQ_SHIFT;

	conf = &pllx_config_table[chiptype][osc_freq];
	/* we are not relocated yet - globals are a bit more tricky here */
	conf = (struct pll_config *)((char *)conf - get_runtime_offset());

	/* set PLL bypass and frequency parameters */
	reg = CRC_PLLX_BASE_BYPASS;
	reg |= (conf->divm << CRC_PLLX_BASE_DIVM_SHIFT) &
		CRC_PLLX_BASE_DIVM_MASK;
	reg |= (conf->divn << CRC_PLLX_BASE_DIVN_SHIFT) &
		CRC_PLLX_BASE_DIVN_MASK;
	reg |= (conf->divp << CRC_PLLX_BASE_DIVP_SHIFT) &
		CRC_PLLX_BASE_DIVP_MASK;
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_PLLX_BASE);

	/* set chargepump parameters */
	reg = (conf->cpcon << CRC_PLLX_MISC_CPCON_SHIFT) &
		CRC_PLLX_MISC_CPCON_MASK;
	if (conf->divn > 600)
		reg |= CRC_PLLX_MISC_DCCON;
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_PLLX_MISC);

	/* enable PLL and disable bypass */
	reg = readl(TEGRA_CLK_RESET_BASE + CRC_PLLX_BASE);
	reg |= CRC_PLLX_BASE_ENABLE;
	reg &= ~CRC_PLLX_BASE_BYPASS;
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_PLLX_BASE);

	/* enable PLL lock */
	reg = readl(TEGRA_CLK_RESET_BASE + CRC_PLLX_MISC);
	reg |= CRC_PLLX_MISC_LOCK_ENABLE;
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_PLLX_MISC);
}

/* start internal and external clocks to core 0 of the main CPU complex */
static void start_cpu0_clocks(void)
{
	u32 reg;

	/* setup PLLX */
	init_pllx();

	/* setup super CLK */
	writel(CRC_SCLK_BURST_POLICY_SYS_STATE_RUN <<
	       CRC_SCLK_BURST_POLICY_SYS_STATE_SHIFT,
	       TEGRA_CLK_RESET_BASE + CRC_SCLK_BURST_POLICY);
	writel(CRC_SUPER_SDIV_ENB, TEGRA_CLK_RESET_BASE + CRC_SUPER_SCLK_DIV);

	writel(1 << CRC_CLK_SYSTEM_RATE_AHB_SHIFT,
	       TEGRA_CLK_RESET_BASE + CRC_CLK_SYSTEM_RATE);

	if (tegra_get_chiptype() >= TEGRA30) {
		/* init MSELECT */
		writel(CRC_RST_DEV_V_MSELECT,
		       TEGRA_CLK_RESET_BASE + CRC_RST_DEV_V_SET);
		writel((CRC_CLK_SOURCE_MSEL_SRC_CLKM <<
		       CRC_CLK_SOURCE_MSEL_SRC_SHIFT),
		       TEGRA_CLK_RESET_BASE + CRC_CLK_SOURCE_MSEL);
		writel(CRC_CLK_OUT_ENB_V_MSELECT,
		       TEGRA_CLK_RESET_BASE + CRC_CLK_OUT_ENB_V_SET);
		tegra_ll_delay_usec(3);
		writel(CRC_RST_DEV_V_MSELECT,
		       TEGRA_CLK_RESET_BASE + CRC_RST_DEV_V_CLR);
	}

	/* deassert clock stop for cpu 0 */
	reg = readl(TEGRA_CLK_RESET_BASE + CRC_CLK_CPU_CMPLX);
	reg &= ~(0xf << 8);
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_CLK_CPU_CMPLX);

	/* enable main CPU complex clock */
	reg = readl(TEGRA_CLK_RESET_BASE + CRC_CLK_OUT_ENB_L);
	reg |= CRC_CLK_OUT_ENB_L_CPU;
	writel(reg, TEGRA_CLK_RESET_BASE + CRC_CLK_OUT_ENB_L);

	/* give clocks some time to settle */
	tegra_ll_delay_usec(300);
}

static void power_up_partition(u32 partid)
{
	u32 reg;

	if (!(readl(TEGRA_PMC_BASE + PMC_PWRGATE_STATUS) & (1 << partid))) {
		writel(PMC_PWRGATE_TOGGLE_START | partid,
		       TEGRA_PMC_BASE + PMC_PWRGATE_TOGGLE);

		while (!(readl(TEGRA_PMC_BASE + PMC_PWRGATE_STATUS) &
			(1 << partid)));

		reg = readl(TEGRA_PMC_BASE + PMC_REMOVE_CLAMPING_CMD);
		reg |= (1 << partid);
		writel(reg, TEGRA_PMC_BASE + PMC_REMOVE_CLAMPING_CMD);

		tegra_ll_delay_usec(1000);
	}
}

static void maincomplex_powerup(void)
{
	/* main cpu rail */
	power_up_partition(PMC_PARTID_CRAIL);

	if (tegra_get_chiptype() >= TEGRA114) {
		/* fast cluster uncore part */
		power_up_partition(PMC_PARTID_C0NC);
		/* fast cluster cpu0 part */
		power_up_partition(PMC_PARTID_CE0);
	}
}

static void tegra_cluster_switch_hp(void)
{
	u32 reg;

	reg = readl(TEGRA_FLOW_CTRL_BASE + FLOW_CLUSTER_CONTROL);
	reg &= ~FLOW_CLUSTER_CONTROL_ACTIVE_LP;
	writel(reg, TEGRA_FLOW_CTRL_BASE + FLOW_CLUSTER_CONTROL);
}

void tegra_avp_reset_vector(uint32_t boarddata)
{
	int num_cores;
	unsigned int entry_address = 0;

	/* put boarddata in scratch reg, for main CPU to fetch after startup */
	writel(boarddata, TEGRA_PMC_BASE + PMC_SCRATCH(10));

	if (tegra_cpu_is_maincomplex())
		tegra_maincomplex_entry();

	/* we want to bring up the high performance CPU complex */
	if (tegra_get_chiptype() >= TEGRA30)
		tegra_cluster_switch_hp();

	/* get the number of cores in the main CPU complex of the current SoC */
	num_cores = tegra_get_num_cores();

	/* bring down main CPU complex (this may be a warm boot) */
	enable_maincomplex_powerrail();
	assert_maincomplex_reset(num_cores);
	stop_maincomplex_clocks(num_cores);

	/* set start address for the main CPU complex processors */
	switch (tegra_get_chiptype()) {
	case TEGRA20:
		entry_address = 0x108000;
		break;
	case TEGRA30:
	case TEGRA124:
		entry_address = 0x80108000;
		break;
	default:
		break;
	}
	writel(entry_address, TEGRA_EXCEPTION_VECTORS_BASE + 0x100);

	/* bring up main CPU complex */
	start_cpu0_clocks();
	maincomplex_powerup();
	deassert_cpu0_reset();

	/* assert AVP reset to stop execution here */
	writel(CRC_RST_DEV_L_COP, TEGRA_CLK_RESET_BASE + CRC_RST_DEV_L_SET);

	unreachable();
}