arm: Tegra2: add support for A9 CPU init
[oweals/u-boot.git] / arch / arm / cpu / armv7 / tegra2 / ap20.c
1 /*
2 * (C) Copyright 2010-2011
3 * NVIDIA Corporation <www.nvidia.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24 #include "ap20.h"
25 #include <asm/io.h>
26 #include <asm/arch/tegra2.h>
27 #include <asm/arch/clk_rst.h>
28 #include <asm/arch/pmc.h>
29 #include <asm/arch/pinmux.h>
30 #include <asm/arch/scu.h>
31 #include <common.h>
32
33 u32 s_first_boot = 1;
34
35 static void enable_cpu_clock(int enable)
36 {
37         struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
38         u32 reg, clk;
39
40         /*
41          * NOTE:
42          * Regardless of whether the request is to enable or disable the CPU
43          * clock, every processor in the CPU complex except the master (CPU 0)
44          * will have it's clock stopped because the AVP only talks to the
45          * master. The AVP does not know (nor does it need to know) that there
46          * are multiple processors in the CPU complex.
47          */
48
49         if (enable) {
50                 /* Wait until all clocks are stable */
51                 udelay(PLL_STABILIZATION_DELAY);
52
53                 writel(CCLK_BURST_POLICY, &clkrst->crc_cclk_brst_pol);
54                 writel(SUPER_CCLK_DIVIDER, &clkrst->crc_super_cclk_div);
55         }
56
57         /* Fetch the register containing the main CPU complex clock enable */
58         reg = readl(&clkrst->crc_clk_out_enb_l);
59         reg |= CLK_ENB_CPU;
60
61         /*
62          * Read the register containing the individual CPU clock enables and
63          * always stop the clock to CPU 1.
64          */
65         clk = readl(&clkrst->crc_clk_cpu_cmplx);
66         clk |= CPU1_CLK_STP;
67
68         if (enable) {
69                 /* Unstop the CPU clock */
70                 clk &= ~CPU0_CLK_STP;
71         } else {
72                 /* Stop the CPU clock */
73                 clk |= CPU0_CLK_STP;
74         }
75
76         writel(clk, &clkrst->crc_clk_cpu_cmplx);
77         writel(reg, &clkrst->crc_clk_out_enb_l);
78 }
79
80 static int is_cpu_powered(void)
81 {
82         struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
83
84         return (readl(&pmc->pmc_pwrgate_status) & CPU_PWRED) ? 1 : 0;
85 }
86
87 static void remove_cpu_io_clamps(void)
88 {
89         struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
90         u32 reg;
91
92         /* Remove the clamps on the CPU I/O signals */
93         reg = readl(&pmc->pmc_remove_clamping);
94         reg |= CPU_CLMP;
95         writel(reg, &pmc->pmc_remove_clamping);
96
97         /* Give I/O signals time to stabilize */
98         udelay(IO_STABILIZATION_DELAY);
99 }
100
101 static void powerup_cpu(void)
102 {
103         struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
104         u32 reg;
105         int timeout = IO_STABILIZATION_DELAY;
106
107         if (!is_cpu_powered()) {
108                 /* Toggle the CPU power state (OFF -> ON) */
109                 reg = readl(&pmc->pmc_pwrgate_toggle);
110                 reg &= PARTID_CP;
111                 reg |= START_CP;
112                 writel(reg, &pmc->pmc_pwrgate_toggle);
113
114                 /* Wait for the power to come up */
115                 while (!is_cpu_powered()) {
116                         if (timeout-- == 0)
117                                 printf("CPU failed to power up!\n");
118                         else
119                                 udelay(10);
120                 }
121
122                 /*
123                  * Remove the I/O clamps from CPU power partition.
124                  * Recommended only on a Warm boot, if the CPU partition gets
125                  * power gated. Shouldn't cause any harm when called after a
126                  * cold boot according to HW, probably just redundant.
127                  */
128                 remove_cpu_io_clamps();
129         }
130 }
131
132 static void enable_cpu_power_rail(void)
133 {
134         struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
135         u32 reg;
136
137         reg = readl(&pmc->pmc_cntrl);
138         reg |= CPUPWRREQ_OE;
139         writel(reg, &pmc->pmc_cntrl);
140
141         /*
142          * The TI PMU65861C needs a 3.75ms delay between enabling
143          * the power rail and enabling the CPU clock.  This delay
144          * between SM1EN and SM1 is for switching time + the ramp
145          * up of the voltage to the CPU (VDD_CPU from PMU).
146          */
147         udelay(3750);
148 }
149
150 static void reset_A9_cpu(int reset)
151 {
152         struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
153         u32 reg, cpu;
154
155         /*
156         * NOTE:  Regardless of whether the request is to hold the CPU in reset
157         *        or take it out of reset, every processor in the CPU complex
158         *        except the master (CPU 0) will be held in reset because the
159         *        AVP only talks to the master. The AVP does not know that there
160         *        are multiple processors in the CPU complex.
161         */
162
163         /* Hold CPU 1 in reset */
164         cpu = SET_DBGRESET1 | SET_DERESET1 | SET_CPURESET1;
165         writel(cpu, &clkrst->crc_cpu_cmplx_set);
166
167         reg = readl(&clkrst->crc_rst_dev_l);
168         if (reset) {
169                 /* Now place CPU0 into reset */
170                 cpu |= SET_DBGRESET0 | SET_DERESET0 | SET_CPURESET0;
171                 writel(cpu, &clkrst->crc_cpu_cmplx_set);
172
173                 /* Enable master CPU reset */
174                 reg |= SWR_CPU_RST;
175         } else {
176                 /* Take CPU0 out of reset */
177                 cpu = CLR_DBGRESET0 | CLR_DERESET0 | CLR_CPURESET0;
178                 writel(cpu, &clkrst->crc_cpu_cmplx_clr);
179
180                 /* Disable master CPU reset */
181                 reg &= ~SWR_CPU_RST;
182         }
183
184         writel(reg, &clkrst->crc_rst_dev_l);
185 }
186
187 static void clock_enable_coresight(int enable)
188 {
189         struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
190         u32 rst, clk, src;
191
192         rst = readl(&clkrst->crc_rst_dev_u);
193         clk = readl(&clkrst->crc_clk_out_enb_u);
194
195         if (enable) {
196                 rst &= ~SWR_CSITE_RST;
197                 clk |= CLK_ENB_CSITE;
198         } else {
199                 rst |= SWR_CSITE_RST;
200                 clk &= ~CLK_ENB_CSITE;
201         }
202
203         writel(clk, &clkrst->crc_clk_out_enb_u);
204         writel(rst, &clkrst->crc_rst_dev_u);
205
206         if (enable) {
207                 /*
208                  * Put CoreSight on PLLP_OUT0 (216 MHz) and divide it down by
209                  *  1.5, giving an effective frequency of 144MHz.
210                  * Set PLLP_OUT0 [bits31:30 = 00], and use a 7.1 divisor
211                  *  (bits 7:0), so 00000001b == 1.5 (n+1 + .5)
212                  */
213                 src = CLK_DIVIDER(NVBL_PLLP_KHZ, 144000);
214                 writel(src, &clkrst->crc_clk_src_csite);
215
216                 /* Unlock the CPU CoreSight interfaces */
217                 rst = 0xC5ACCE55;
218                 writel(rst, CSITE_CPU_DBG0_LAR);
219                 writel(rst, CSITE_CPU_DBG1_LAR);
220         }
221 }
222
223 void start_cpu(u32 reset_vector)
224 {
225         /* Enable VDD_CPU */
226         enable_cpu_power_rail();
227
228         /* Hold the CPUs in reset */
229         reset_A9_cpu(1);
230
231         /* Disable the CPU clock */
232         enable_cpu_clock(0);
233
234         /* Enable CoreSight */
235         clock_enable_coresight(1);
236
237         /*
238          * Set the entry point for CPU execution from reset,
239          *  if it's a non-zero value.
240          */
241         if (reset_vector)
242                 writel(reset_vector, EXCEP_VECTOR_CPU_RESET_VECTOR);
243
244         /* Enable the CPU clock */
245         enable_cpu_clock(1);
246
247         /* If the CPU doesn't already have power, power it up */
248         powerup_cpu();
249
250         /* Take the CPU out of reset */
251         reset_A9_cpu(0);
252 }
253
254
255 void halt_avp(void)
256 {
257         for (;;) {
258                 writel((HALT_COP_EVENT_JTAG | HALT_COP_EVENT_IRQ_1 \
259                         | HALT_COP_EVENT_FIQ_1 | (FLOW_MODE_STOP<<29)),
260                         FLOW_CTLR_HALT_COP_EVENTS);
261         }
262 }
263
264 void enable_scu(void)
265 {
266         struct scu_ctlr *scu = (struct scu_ctlr *)NV_PA_ARM_PERIPHBASE;
267         u32 reg;
268
269         /* If SCU already setup/enabled, return */
270         if (readl(&scu->scu_ctrl) & SCU_CTRL_ENABLE)
271                 return;
272
273         /* Invalidate all ways for all processors */
274         writel(0xFFFF, &scu->scu_inv_all);
275
276         /* Enable SCU - bit 0 */
277         reg = readl(&scu->scu_ctrl);
278         reg |= SCU_CTRL_ENABLE;
279         writel(reg, &scu->scu_ctrl);
280 }
281
282 void init_pmc_scratch(void)
283 {
284         struct pmc_ctlr *const pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
285         int i;
286
287         /* SCRATCH0 is initialized by the boot ROM and shouldn't be cleared */
288         for (i = 0; i < 23; i++)
289                 writel(0, &pmc->pmc_scratch1+i);
290
291         /* ODMDATA is for kernel use to determine RAM size, LP config, etc. */
292         writel(CONFIG_SYS_BOARD_ODMDATA, &pmc->pmc_scratch20);
293 }
294
295 void cpu_start(void)
296 {
297         struct pmux_tri_ctlr *pmt = (struct pmux_tri_ctlr *)NV_PA_APB_MISC_BASE;
298
299         /* enable JTAG */
300         writel(0xC0, &pmt->pmt_cfg_ctl);
301
302         if (s_first_boot) {
303                 /*
304                  * Need to set this before cold-booting,
305                  *  otherwise we'll end up in an infinite loop.
306                  */
307                 s_first_boot = 0;
308                 cold_boot();
309         }
310 }
311
312 void tegra2_start()
313 {
314         if (s_first_boot) {
315                 /* Init Debug UART Port (115200 8n1) */
316                 uart_init();
317
318                 /* Init PMC scratch memory */
319                 init_pmc_scratch();
320         }
321
322 #ifdef CONFIG_ENABLE_CORTEXA9
323         /* take the mpcore out of reset */
324         cpu_start();
325
326         /* configure cache */
327         cache_configure();
328 #endif
329 }