summaryrefslogtreecommitdiff
path: root/board/nvidia/common/board.c
blob: 5cb219b4f9211fd407d36a90831f0d2cae12eab6 (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
/*
 *  (C) Copyright 2010,2011
 *  NVIDIA Corporation <www.nvidia.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <ns16550.h>
#include <asm/clocks.h>
#include <asm/io.h>

/* TBD: bring these over when Tegra3 is ready, then remove these #ifdefs */
#include <asm/arch-tegra/bitfield.h>
#include <asm/arch-tegra/clk_rst.h>
#include <asm/arch-tegra/pmc.h>
#include <asm/arch-tegra/uart.h>
#include <asm/arch-tegra/warmboot.h>
#include <asm/arch/clock.h>
#ifdef CONFIG_TEGRA2
#include <asm/arch/emc.h>
#include <asm/arch/gpio.h>
#endif
#include <asm/arch/pinmux.h>
#include <asm/arch/sys_proto.h>
#ifdef CONFIG_USB_EHCI_TEGRA
#include <asm/arch/usb.h>
#endif
#include <asm/arch/tegra.h>

#ifdef CONFIG_TEGRA_SPI
#include <spi.h>
#endif
#ifdef CONFIG_TEGRA_I2C
#include <i2c.h>
#endif
#include "board.h"
#include "pmu.h"

#ifdef CONFIG_TEGRA_MMC
#include <asm/arch/pmu.h>
#include <mmc.h>
#endif
#ifdef CONFIG_OF_CONTROL
#include <fdt_decode.h>
#include <libfdt.h>
#endif

#ifdef CONFIG_CHROMEOS
#include <chromeos/common.h>
#endif

DECLARE_GLOBAL_DATA_PTR;

#if defined(CONFIG_TEGRA_CLOCK_SCALING) && !defined(CONFIG_TEGRA_I2C)
#error "tegra: We need CONFIG_TEGRA_I2C to support CONFIG_TEGRA_CLOCK_SCALING"
#endif

#if defined(CONFIG_TEGRA_CLOCK_SCALING) && !defined(CONFIG_TEGRA_PMU)
#error "tegra: We need CONFIG_TEGRA_PMU to support CONFIG_TEGRA_CLOCK_SCALING"
#endif

enum {
	/* UARTs which we can enable */
	UARTA	= 1 << 0,
	UARTB	= 1 << 1,
	UARTD	= 1 << 3,
	UART_ALL	= 0xf
};

#ifndef CONFIG_OF_CONTROL
const struct tegra_sysinfo sysinfo = {
	CONFIG_TEGRA_BOARD_STRING
};
#endif

/*
 * Routine: timer_init
 * Description: init the timestamp and lastinc value
 */
int timer_init(void)
{
	reset_timer();
	return 0;
}

static void enable_uart(enum periph_id pid)
{
	/* Assert UART reset and enable clock */
	reset_set_enable(pid, 1);
	clock_enable(pid);
	clock_ll_set_source(pid, 0);	/* UARTx_CLK_SRC = 00, PLLP_OUT0 */

	/* wait for 2us */
	udelay(2);

	/* De-assert reset to UART */
	reset_set_enable(pid, 0);
}

/*
 * Routine: clock_init_uart
 * Description: init clock for the UART(s)
 */
static void clock_init_uart(int uart_ids)
{
	if (uart_ids & UARTA)
		enable_uart(PERIPH_ID_UART1);
	if (uart_ids & UARTB)
		enable_uart(PERIPH_ID_UART2);
	if (uart_ids & UARTD)
		enable_uart(PERIPH_ID_UART4);
}

/*
 * Routine: pin_mux_uart
 * Description: setup the pin muxes/tristate values for the UART(s)
 */
static void pin_mux_uart(int uart_ids)
{
#if defined(CONFIG_TEGRA2)
	if (uart_ids & UARTA) {
		pinmux_set_func(PINGRP_IRRX, PMUX_FUNC_UARTA);
		pinmux_set_func(PINGRP_IRTX, PMUX_FUNC_UARTA);
		pinmux_tristate_disable(PINGRP_IRRX);
		pinmux_tristate_disable(PINGRP_IRTX);
	}
	if (uart_ids & UARTB) {
		pinmux_set_func(PINGRP_UAD, PMUX_FUNC_IRDA);
		pinmux_tristate_disable(PINGRP_UAD);
	}
	if (uart_ids & UARTD) {
		pinmux_set_func(PINGRP_GMC, PMUX_FUNC_UARTD);
		pinmux_tristate_disable(PINGRP_GMC);
	}
#endif	/* CONFIG_TEGRA2 */
}

/*
 * Routine: pin_mux_switches
 * Description: Disable internal pullups for the write protect, SDIO3 write
 * protect and the Google Recovery switch.  All of these switches have external
 * pull ups or pull downs.
 */
static void pin_mux_switches(void)
{
#if defined(CONFIG_TEGRA2)
	/*
	 * TODO(robotboy): Move this to the FDT once there is pin mux support
	 * there.  Currently all Tegra based boards use the same GPIOs for
	 * these switches.
	 */
	pinmux_set_pullupdown(PINGRP_ATD, PMUX_PULL_NORMAL);
#endif	/* CONFIG_TEGRA2 */
}

#ifdef CONFIG_TEGRA_MMC
/*
 * Routine: pin_mux_mmc
 * Description: setup the pin muxes/tristate values for the SDMMC(s)
 */
static void pin_mux_mmc(void)
{
#ifdef CONFIG_TEGRA2
	/* SDMMC4: config 3, x8 on 2nd set of pins */
	pinmux_set_func(PINGRP_ATB, PMUX_FUNC_SDIO4);
	pinmux_set_func(PINGRP_GMA, PMUX_FUNC_SDIO4);
	pinmux_set_func(PINGRP_GME, PMUX_FUNC_SDIO4);

	pinmux_tristate_disable(PINGRP_ATB);
	pinmux_tristate_disable(PINGRP_GMA);
	pinmux_tristate_disable(PINGRP_GME);

	/* SDMMC3: SDIO3_CLK, SDIO3_CMD, SDIO3_DAT[3:0] */
	pinmux_set_func(PINGRP_SDB, PMUX_FUNC_SDIO3);
	pinmux_set_func(PINGRP_SDC, PMUX_FUNC_SDIO3);
	pinmux_set_func(PINGRP_SDD, PMUX_FUNC_SDIO3);

	pinmux_set_func(PINGRP_SLXA, PMUX_FUNC_SDIO3);
	pinmux_set_func(PINGRP_SLXC, PMUX_FUNC_SDIO3);
	pinmux_set_func(PINGRP_SLXD, PMUX_FUNC_SDIO3);
	pinmux_set_func(PINGRP_SLXK, PMUX_FUNC_SDIO3);

	pinmux_tristate_disable(PINGRP_SDC);
	pinmux_tristate_disable(PINGRP_SDD);
	pinmux_tristate_disable(PINGRP_SDB);

	pinmux_tristate_disable(PINGRP_SLXA);
	pinmux_tristate_disable(PINGRP_SLXC);
	pinmux_tristate_disable(PINGRP_SLXD);
	pinmux_tristate_disable(PINGRP_SLXK);
#endif
}
#endif

#ifdef CONFIG_TEGRA3
#include "../cardhu/pinmux-config-common.h"
#endif

/*
 * Routine: pinmux_init
 * Description: Do individual peripheral pinmux configs
 */
static void pinmux_init(void)
{
	pin_mux_switches();

#if defined(CONFIG_TEGRA3)
	pinmux_config_table(tegra3_pinmux_common,
				ARRAY_SIZE(tegra3_pinmux_common));

	pinmux_config_table(unused_pins_lowpower,
				ARRAY_SIZE(unused_pins_lowpower));
#endif
}

static void init_uarts(const void *blob)
{
	int uart_ids = 0;	/* bit mask of which UART ids to enable */
#ifdef CONFIG_OF_CONTROL
	struct fdt_uart uart;

	if (!fdt_decode_uart_console(blob, &uart, gd->baudrate))
		uart_ids = 1 << uart.id;
#else
#ifdef CONFIG_TEGRA2_ENABLE_UARTA
	uart_ids |= UARTA;
#endif
#ifdef CONFIG_TEGRA2_ENABLE_UARTB
	uart_ids |= UARTB;
#endif
#ifdef CONFIG_TEGRA2_ENABLE_UARTD
	uart_ids |= UARTD;
#endif
#endif /* CONFIG_OF_CONTROL */
	/* Initialize UART clocks */
	clock_init_uart(uart_ids);

	/* Initialize periph pinmuxes */
#if defined(CONFIG_TEGRA2)
	pin_mux_uart(uart_ids);
#endif

	/* Initialize periph GPIOs */
#ifdef CONFIG_SPI_UART_SWITCH
	gpio_early_init_uart(blob);
#endif
}

/*
 * Do I2C/PMU writes to bring up SD card bus power
 *
 */
static void board_sdmmc_voltage_init(void)
{
#if defined(CONFIG_TEGRA3) && defined(CONFIG_TEGRA_MMC)
	/*
	 * Voltage for SDMMC on Tegra30 Cardhu variants is on
	 * LDO5 and should be at 3.3.
	 *
	 * TODO(dianders): Should be in device tree.
	 */
	uchar ldo5_to_3_3v = PMU_LDO5_SEL(33) | PMU_LDO5_ON;

	pmu_write(PMU_LDO5_REG, &ldo5_to_3_3v, 1);
#endif
}

/*
 * Routine: power_det_init
 * Description: turn off power detects
 */
static void power_det_init(void)
{
#if defined(CONFIG_TEGRA2)
	struct pmc_ctlr *const pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;

	/* turn off power detects */
	writel(0, &pmc->pmc_pwr_det_latch);
	writel(0, &pmc->pmc_pwr_det);
#endif
}

/*
 * Routine: board_init
 * Description: Early hardware init.
 */
int board_init(void)
{
#ifdef CONFIG_VIDEO_TEGRA
	tegra_lcd_check_next_stage(gd->blob, 0);
#endif
#ifdef CONFIG_DELAY_CONSOLE
	init_uarts(gd->blob);
#endif
	/* Do clocks and UART first so that printf() works */
	clock_init();
#ifdef CONFIG_SPI_UART_SWITCH
	gpio_config_uart(gd->blob);
#endif
#ifdef CONFIG_USB_EHCI_TEGRA
	board_usb_init(gd->blob);
#endif
	clock_verify();
#ifdef CONFIG_TEGRA_SPI
	spi_init();
#endif
	power_det_init();

#ifdef CONFIG_TEGRA_I2C
	/* Ramp up the core voltage, then change to full CPU speed */
	i2c_init_board();
#endif

#ifdef CONFIG_TEGRA_CLOCK_SCALING
	pmu_set_nominal();
	arch_full_speed();
#endif

	/* board id for Linux */
#ifdef CONFIG_OF_CONTROL
	gd->bd->bi_arch_number = fdt_decode_get_machine_arch_id(gd->blob);
	if (gd->bd->bi_arch_number == -1U)
		printf("Warning: No /config/machine-arch-id defined in fdt\n");
#else
	gd->bd->bi_arch_number = CONFIG_MACH_TYPE;
#endif

#ifdef CONFIG_TEGRA_CLOCK_SCALING
	board_emc_init();
#endif

#ifdef CONFIG_TEGRA_LP0
	/* prepare the WB code to LP0 location */
	warmboot_prepare_code(TEGRA_LP0_ADDR, TEGRA_LP0_SIZE);
#endif

	board_sdmmc_voltage_init();

	/* boot param addr */
	gd->bd->bi_boot_params = (NV_PA_SDRAM_BASE + 0x100);

#ifdef CONFIG_CHROMEOS
	vbexport_init();
#endif

	return 0;
}

#ifdef CONFIG_BOARD_EARLY_INIT_F

int board_early_init_f(void)
{
	ulong pllp_rate = 216000000;	/* default PLLP clock rate */

	/* Initialize essential common plls */
#ifdef CONFIG_OF_CONTROL
	pllp_rate = fdt_decode_clock_rate(gd->blob, "pllp", pllp_rate);
#endif
	clock_early_init(pllp_rate);

	pinmux_init();
#ifndef CONFIG_DELAY_CONSOLE
	init_uarts(gd->blob);
#endif

#ifdef CONFIG_VIDEO_TEGRA
	/* Get LCD panel size */
	lcd_early_init(gd->blob);
#endif

	return 0;
}
#endif	/* EARLY_INIT */

#ifdef CONFIG_TEGRA_MMC
/* this is a weak define that we are overriding */
int board_mmc_init(bd_t *bd)
{
	debug("board_mmc_init called\n");

	/* Enable muxes, etc. for SDMMC controllers */
	pin_mux_mmc();

	tegra_mmc_init(gd->blob);

	return 0;
}
#endif

/*
 * Possible UART locations: we ignore UARTC at 0x70006200 and UARTE at
 * 0x70006400, since we don't have code to init them
 */
static u32 uart_reg_addr[] = {
	NV_PA_APB_UARTA_BASE,
	NV_PA_APB_UARTB_BASE,
	NV_PA_APB_UARTD_BASE,
	0
};

/**
 * Send out serial output wherever we can.
 *
 * This function produces a low-level panic message, after setting PLLP
 * to the given value.
 *
 * @param pllp_rate	Required PLLP rate (408000000 or 216000000)
 * @param str		String to output
 */
static void send_output_with_pllp(ulong pllp_rate, const char *str)
{
	int uart_ids = UART_ALL;	/* turn it all on! */
	u32 *uart_addr;
	int clock_freq, multiplier, baudrate, divisor;

	clock_early_init(pllp_rate);

	/* Try to enable all possible UARTs */
	clock_init_uart(uart_ids);
	pin_mux_uart(uart_ids);
#ifdef CONFIG_TEGRA3
	/* Until we sort out pinmux, we must do the global Tegra3 init */
	pinmux_init();
#endif

	/*
	 * Seaboard has a UART switch on PI3. We might be a Seaboard,
	 * so flip it!
	 */
#ifdef CONFIG_SPI_UART_SWITCH
	gpio_direction_output(GPIO_PI3, 0);
#endif

	/*
	 * Now send the string out all the Tegra UARTs. We don't try all
	 * possible configurations, but this could be added if required.
	 */
	clock_freq = pllp_rate;
	multiplier = CONFIG_DEFAULT_NS16550_MULT;
	baudrate = CONFIG_BAUDRATE;
	divisor = (clock_freq + (baudrate * (multiplier / 2))) /
			(multiplier * baudrate);

	for (uart_addr = uart_reg_addr; *uart_addr; uart_addr++) {
		const char *s;

		NS16550_init((NS16550_t)*uart_addr, divisor);
		for (s = str; *s; s++) {
			NS16550_putc((NS16550_t)*uart_addr, *s);
			if (*s == '\n')
				NS16550_putc((NS16550_t)*uart_addr, '\r');
		}
	}
}

/*
 * This is called when we have no console. About the only reason that this
 * happen is if we don't have a valid fdt. So we don't know what kind of
 * Tegra board we are. We blindly try to print a message every which way we
 * know.
 */
void board_panic_no_console(const char *str)
{
	/* We don't know what PLLP to use, so try both */
	send_output_with_pllp(216000000, str);
	send_output_with_pllp(408000000, str);
}

int board_late_init(void)
{
	/* Make sure we finish initing the LCD */
#ifdef CONFIG_VIDEO_TEGRA
	tegra_lcd_check_next_stage(gd->blob, 1);
#endif
	return 0;
}