1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2013
3*4882a593Smuzhiyun * NVIDIA Corporation <www.nvidia.com>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <asm/io.h>
10*4882a593Smuzhiyun #include <asm/arch/ahb.h>
11*4882a593Smuzhiyun #include <asm/arch/clock.h>
12*4882a593Smuzhiyun #include <asm/arch/flow.h>
13*4882a593Smuzhiyun #include <asm/arch/pinmux.h>
14*4882a593Smuzhiyun #include <asm/arch/tegra.h>
15*4882a593Smuzhiyun #include <asm/arch-tegra/clk_rst.h>
16*4882a593Smuzhiyun #include <asm/arch-tegra/pmc.h>
17*4882a593Smuzhiyun #include <asm/arch-tegra/ap.h>
18*4882a593Smuzhiyun #include "../cpu.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /* Tegra124-specific CPU init code */
21*4882a593Smuzhiyun
enable_cpu_power_rail(void)22*4882a593Smuzhiyun static void enable_cpu_power_rail(void)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun debug("%s entry\n", __func__);
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* un-tristate PWR_I2C SCL/SDA, rest of the defaults are correct */
29*4882a593Smuzhiyun pinmux_tristate_disable(PMUX_PINGRP_PWR_I2C_SCL_PZ6);
30*4882a593Smuzhiyun pinmux_tristate_disable(PMUX_PINGRP_PWR_I2C_SDA_PZ7);
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun pmic_enable_cpu_vdd();
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * Set CPUPWRGOOD_TIMER - APB clock is 1/2 of SCLK (102MHz),
36*4882a593Smuzhiyun * set it for 5ms as per SysEng (102MHz*5ms = 510000 (7C830h).
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun writel(0x7C830, &pmc->pmc_cpupwrgood_timer);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Set polarity to 0 (normal) and enable CPUPWRREQ_OE */
41*4882a593Smuzhiyun clrbits_le32(&pmc->pmc_cntrl, CPUPWRREQ_POL);
42*4882a593Smuzhiyun setbits_le32(&pmc->pmc_cntrl, CPUPWRREQ_OE);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
enable_cpu_clocks(void)45*4882a593Smuzhiyun static void enable_cpu_clocks(void)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
48*4882a593Smuzhiyun struct clk_pll_info *pllinfo = &tegra_pll_info_table[CLOCK_ID_XCPU];
49*4882a593Smuzhiyun u32 reg;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun debug("%s entry\n", __func__);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* Wait for PLL-X to lock */
54*4882a593Smuzhiyun do {
55*4882a593Smuzhiyun reg = readl(&clkrst->crc_pll_simple[SIMPLE_PLLX].pll_base);
56*4882a593Smuzhiyun debug("%s: PLLX base = 0x%08X\n", __func__, reg);
57*4882a593Smuzhiyun } while ((reg & (1 << pllinfo->lock_det)) == 0);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun debug("%s: PLLX locked, delay for stable clocks\n", __func__);
60*4882a593Smuzhiyun /* Wait until all clocks are stable */
61*4882a593Smuzhiyun udelay(PLL_STABILIZATION_DELAY);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun debug("%s: Setting CCLK_BURST and DIVIDER\n", __func__);
64*4882a593Smuzhiyun writel(CCLK_BURST_POLICY, &clkrst->crc_cclk_brst_pol);
65*4882a593Smuzhiyun writel(SUPER_CCLK_DIVIDER, &clkrst->crc_super_cclk_div);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun debug("%s: Enabling clock to all CPUs\n", __func__);
68*4882a593Smuzhiyun /* Enable the clock to all CPUs */
69*4882a593Smuzhiyun reg = CLR_CPU3_CLK_STP | CLR_CPU2_CLK_STP | CLR_CPU1_CLK_STP |
70*4882a593Smuzhiyun CLR_CPU0_CLK_STP;
71*4882a593Smuzhiyun writel(reg, &clkrst->crc_clk_cpu_cmplx_clr);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun debug("%s: Enabling main CPU complex clocks\n", __func__);
74*4882a593Smuzhiyun /* Always enable the main CPU complex clocks */
75*4882a593Smuzhiyun clock_enable(PERIPH_ID_CPU);
76*4882a593Smuzhiyun clock_enable(PERIPH_ID_CPULP);
77*4882a593Smuzhiyun clock_enable(PERIPH_ID_CPUG);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun debug("%s: Done\n", __func__);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
remove_cpu_resets(void)82*4882a593Smuzhiyun static void remove_cpu_resets(void)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
85*4882a593Smuzhiyun u32 reg;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun debug("%s entry\n", __func__);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* Take the slow and fast partitions out of reset */
90*4882a593Smuzhiyun reg = CLR_NONCPURESET;
91*4882a593Smuzhiyun writel(reg, &clkrst->crc_rst_cpulp_cmplx_clr);
92*4882a593Smuzhiyun writel(reg, &clkrst->crc_rst_cpug_cmplx_clr);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* Clear the SW-controlled reset of the slow cluster */
95*4882a593Smuzhiyun reg = CLR_CPURESET0 | CLR_DBGRESET0 | CLR_CORERESET0 | CLR_CXRESET0 |
96*4882a593Smuzhiyun CLR_L2RESET | CLR_PRESETDBG;
97*4882a593Smuzhiyun writel(reg, &clkrst->crc_rst_cpulp_cmplx_clr);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* Clear the SW-controlled reset of the fast cluster */
100*4882a593Smuzhiyun reg = CLR_CPURESET0 | CLR_DBGRESET0 | CLR_CORERESET0 | CLR_CXRESET0 |
101*4882a593Smuzhiyun CLR_CPURESET1 | CLR_DBGRESET1 | CLR_CORERESET1 | CLR_CXRESET1 |
102*4882a593Smuzhiyun CLR_CPURESET2 | CLR_DBGRESET2 | CLR_CORERESET2 | CLR_CXRESET2 |
103*4882a593Smuzhiyun CLR_CPURESET3 | CLR_DBGRESET3 | CLR_CORERESET3 | CLR_CXRESET3 |
104*4882a593Smuzhiyun CLR_L2RESET | CLR_PRESETDBG;
105*4882a593Smuzhiyun writel(reg, &clkrst->crc_rst_cpug_cmplx_clr);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * Tegra124 requires some special clock initialization, including setting up
110*4882a593Smuzhiyun * the DVC I2C, turning on MSELECT and selecting the G CPU cluster
111*4882a593Smuzhiyun */
tegra124_init_clocks(void)112*4882a593Smuzhiyun void tegra124_init_clocks(void)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun struct flow_ctlr *flow = (struct flow_ctlr *)NV_PA_FLOW_BASE;
115*4882a593Smuzhiyun struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
116*4882a593Smuzhiyun struct clk_rst_ctlr *clkrst =
117*4882a593Smuzhiyun (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
118*4882a593Smuzhiyun u32 val;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun debug("%s entry\n", __func__);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* Set active CPU cluster to G */
123*4882a593Smuzhiyun clrbits_le32(&flow->cluster_control, 1);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Change the oscillator drive strength */
126*4882a593Smuzhiyun val = readl(&clkrst->crc_osc_ctrl);
127*4882a593Smuzhiyun val &= ~OSC_XOFS_MASK;
128*4882a593Smuzhiyun val |= (OSC_DRIVE_STRENGTH << OSC_XOFS_SHIFT);
129*4882a593Smuzhiyun writel(val, &clkrst->crc_osc_ctrl);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* Update same value in PMC_OSC_EDPD_OVER XOFS field for warmboot */
132*4882a593Smuzhiyun val = readl(&pmc->pmc_osc_edpd_over);
133*4882a593Smuzhiyun val &= ~PMC_XOFS_MASK;
134*4882a593Smuzhiyun val |= (OSC_DRIVE_STRENGTH << PMC_XOFS_SHIFT);
135*4882a593Smuzhiyun writel(val, &pmc->pmc_osc_edpd_over);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /* Set HOLD_CKE_LOW_EN to 1 */
138*4882a593Smuzhiyun setbits_le32(&pmc->pmc_cntrl2, HOLD_CKE_LOW_EN);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun debug("Setting up PLLX\n");
141*4882a593Smuzhiyun init_pllx();
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun val = (1 << CLK_SYS_RATE_AHB_RATE_SHIFT);
144*4882a593Smuzhiyun writel(val, &clkrst->crc_clk_sys_rate);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Enable clocks to required peripherals. TBD - minimize this list */
147*4882a593Smuzhiyun debug("Enabling clocks\n");
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_CACHE2, 1);
150*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_GPIO, 1);
151*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_TMR, 1);
152*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_CPU, 1);
153*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_EMC, 1);
154*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_I2C5, 1);
155*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_APBDMA, 1);
156*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_MEM, 1);
157*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_CORESIGHT, 1);
158*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_MSELECT, 1);
159*4882a593Smuzhiyun clock_set_enable(PERIPH_ID_DVFS, 1);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Set MSELECT clock source as PLLP (00), and ask for a clock
163*4882a593Smuzhiyun * divider that would set the MSELECT clock at 102MHz for a
164*4882a593Smuzhiyun * PLLP base of 408MHz.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun clock_ll_set_source_divisor(PERIPH_ID_MSELECT, 0,
167*4882a593Smuzhiyun CLK_DIVIDER(NVBL_PLLP_KHZ, 102000));
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* Give clock time to stabilize */
170*4882a593Smuzhiyun udelay(IO_STABILIZATION_DELAY);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* I2C5 (DVC) gets CLK_M and a divisor of 17 */
173*4882a593Smuzhiyun clock_ll_set_source_divisor(PERIPH_ID_I2C5, 3, 16);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* Give clock time to stabilize */
176*4882a593Smuzhiyun udelay(IO_STABILIZATION_DELAY);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* Take required peripherals out of reset */
179*4882a593Smuzhiyun debug("Taking periphs out of reset\n");
180*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_CACHE2, 0);
181*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_GPIO, 0);
182*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_TMR, 0);
183*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_COP, 0);
184*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_EMC, 0);
185*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_I2C5, 0);
186*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_APBDMA, 0);
187*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_MEM, 0);
188*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_CORESIGHT, 0);
189*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_MSELECT, 0);
190*4882a593Smuzhiyun reset_set_enable(PERIPH_ID_DVFS, 0);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun debug("%s exit\n", __func__);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
is_partition_powered(u32 partid)195*4882a593Smuzhiyun static bool is_partition_powered(u32 partid)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
198*4882a593Smuzhiyun u32 reg;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* Get power gate status */
201*4882a593Smuzhiyun reg = readl(&pmc->pmc_pwrgate_status);
202*4882a593Smuzhiyun return !!(reg & (1 << partid));
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
power_partition(u32 partid)205*4882a593Smuzhiyun static void power_partition(u32 partid)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun debug("%s: part ID = %08X\n", __func__, partid);
210*4882a593Smuzhiyun /* Is the partition already on? */
211*4882a593Smuzhiyun if (!is_partition_powered(partid)) {
212*4882a593Smuzhiyun /* No, toggle the partition power state (OFF -> ON) */
213*4882a593Smuzhiyun debug("power_partition, toggling state\n");
214*4882a593Smuzhiyun writel(START_CP | partid, &pmc->pmc_pwrgate_toggle);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /* Wait for the power to come up */
217*4882a593Smuzhiyun while (!is_partition_powered(partid))
218*4882a593Smuzhiyun ;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* Give I/O signals time to stabilize */
221*4882a593Smuzhiyun udelay(IO_STABILIZATION_DELAY);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
powerup_cpus(void)225*4882a593Smuzhiyun void powerup_cpus(void)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun /* We boot to the fast cluster */
228*4882a593Smuzhiyun debug("%s entry: G cluster\n", __func__);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* Power up the fast cluster rail partition */
231*4882a593Smuzhiyun debug("%s: CRAIL\n", __func__);
232*4882a593Smuzhiyun power_partition(CRAIL);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* Power up the fast cluster non-CPU partition */
235*4882a593Smuzhiyun debug("%s: C0NC\n", __func__);
236*4882a593Smuzhiyun power_partition(C0NC);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* Power up the fast cluster CPU0 partition */
239*4882a593Smuzhiyun debug("%s: CE0\n", __func__);
240*4882a593Smuzhiyun power_partition(CE0);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun debug("%s: done\n", __func__);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
start_cpu(u32 reset_vector)245*4882a593Smuzhiyun void start_cpu(u32 reset_vector)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun debug("%s entry, reset_vector = %x\n", __func__, reset_vector);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun tegra124_init_clocks();
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /* Set power-gating timer multiplier */
254*4882a593Smuzhiyun writel((MULT_8 << TIMER_MULT_SHIFT) | (MULT_8 << TIMER_MULT_CPU_SHIFT),
255*4882a593Smuzhiyun &pmc->pmc_pwrgate_timer_mult);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun enable_cpu_power_rail();
258*4882a593Smuzhiyun enable_cpu_clocks();
259*4882a593Smuzhiyun clock_enable_coresight(1);
260*4882a593Smuzhiyun remove_cpu_resets();
261*4882a593Smuzhiyun writel(reset_vector, EXCEP_VECTOR_CPU_RESET_VECTOR);
262*4882a593Smuzhiyun powerup_cpus();
263*4882a593Smuzhiyun debug("%s exit, should continue @ reset_vector\n", __func__);
264*4882a593Smuzhiyun }
265