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