1 /*
2  * (C) Copyright 2014
3  * NVIDIA Corporation <www.nvidia.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <errno.h>
10 #include <asm/gpio.h>
11 #include <asm/io.h>
12 #include <asm/arch/pinmux.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch/mc.h>
15 #include <asm/arch-tegra/clk_rst.h>
16 #include <asm/arch-tegra/pmc.h>
17 #include <power/as3722.h>
18 #include <power/pmic.h>
19 #include "pinmux-config-nyan-big.h"
20 
21 /*
22  * Routine: pinmux_init
23  * Description: Do individual peripheral pinmux configs
24  */
25 void pinmux_init(void)
26 {
27 	gpio_config_table(nyan_big_gpio_inits,
28 			  ARRAY_SIZE(nyan_big_gpio_inits));
29 
30 	pinmux_config_pingrp_table(nyan_big_pingrps,
31 				   ARRAY_SIZE(nyan_big_pingrps));
32 
33 	pinmux_config_drvgrp_table(nyan_big_drvgrps,
34 				   ARRAY_SIZE(nyan_big_drvgrps));
35 }
36 
37 int tegra_board_id(void)
38 {
39 	static const int vector[] = {TEGRA_GPIO(Q, 3), TEGRA_GPIO(T, 1),
40 					TEGRA_GPIO(X, 1), TEGRA_GPIO(X, 4),
41 					-1};
42 
43 	gpio_claim_vector(vector, "board_id%d");
44 	return gpio_get_values_as_int(vector);
45 }
46 
47 int tegra_lcd_pmic_init(int board_id)
48 {
49 	struct udevice *pmic;
50 	int ret;
51 
52 	ret = as3722_get(&pmic);
53 	if (ret)
54 		return -ENOENT;
55 
56 	if (board_id == 0)
57 		as3722_write(pmic, 0x00, 0x3c);
58 	else
59 		as3722_write(pmic, 0x00, 0x50);
60 	as3722_write(pmic, 0x12, 0x10);
61 	as3722_write(pmic, 0x0c, 0x07);
62 	as3722_write(pmic, 0x20, 0x10);
63 
64 	return 0;
65 }
66 
67 /* Setup required information for Linux kernel */
68 static void setup_kernel_info(void)
69 {
70 	struct mc_ctlr *mc = (void *)NV_PA_MC_BASE;
71 
72 	/* The kernel graphics driver needs this region locked down */
73 	writel(0, &mc->mc_video_protect_bom);
74 	writel(0, &mc->mc_video_protect_size_mb);
75 	writel(1, &mc->mc_video_protect_reg_ctrl);
76 }
77 
78 /*
79  * We need to take ALL audio devices conntected to AHUB (AUDIO, APBIF,
80  * I2S, DAM, AMX, ADX, SPDIF, AFC) out of reset and enable the clocks.
81  * Otherwise reading AHUB devices will hang when the kernel boots.
82  */
83 static void enable_required_clocks(void)
84 {
85 	static enum periph_id ids[] = {
86 		PERIPH_ID_I2S0,
87 		PERIPH_ID_I2S1,
88 		PERIPH_ID_I2S2,
89 		PERIPH_ID_I2S3,
90 		PERIPH_ID_I2S4,
91 		PERIPH_ID_AUDIO,
92 		PERIPH_ID_APBIF,
93 		PERIPH_ID_DAM0,
94 		PERIPH_ID_DAM1,
95 		PERIPH_ID_DAM2,
96 		PERIPH_ID_AMX0,
97 		PERIPH_ID_AMX1,
98 		PERIPH_ID_ADX0,
99 		PERIPH_ID_ADX1,
100 		PERIPH_ID_SPDIF,
101 		PERIPH_ID_AFC0,
102 		PERIPH_ID_AFC1,
103 		PERIPH_ID_AFC2,
104 		PERIPH_ID_AFC3,
105 		PERIPH_ID_AFC4,
106 		PERIPH_ID_AFC5,
107 		PERIPH_ID_EXTPERIPH1
108 	};
109 	int i;
110 
111 	for (i = 0; i < ARRAY_SIZE(ids); i++)
112 		clock_enable(ids[i]);
113 	udelay(2);
114 	for (i = 0; i < ARRAY_SIZE(ids); i++)
115 		reset_set_enable(ids[i], 0);
116 }
117 
118 int nvidia_board_init(void)
119 {
120 	clock_start_periph_pll(PERIPH_ID_EXTPERIPH1, CLOCK_ID_OSC, 12000000);
121 	clock_start_periph_pll(PERIPH_ID_I2S1, CLOCK_ID_OSC, 1500000);
122 
123 	/* For external MAX98090 audio codec */
124 	clock_external_output(1);
125 	setup_kernel_info();
126 	enable_required_clocks();
127 
128 	return 0;
129 }
130