blob: 581f05726996dd4f1a8515b5ddf75106e1fcfa56 [file] [log] [blame]
Daniel Hellstrom207e6952008-03-28 10:00:33 +01001/* Initializes CPU and basic hardware such as memory
2 * controllers, IRQ controller and system timer 0.
3 *
Francois Retiefe3051d92015-10-28 14:29:32 +02004 * (C) Copyright 2007, 2015
5 * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com
Daniel Hellstrom207e6952008-03-28 10:00:33 +01006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Daniel Hellstrom207e6952008-03-28 10:00:33 +01008 */
9
10#include <common.h>
11#include <asm/asi.h>
12#include <asm/leon.h>
13
14#include <config.h>
15
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +020016#define TIMER_BASE_CLK 1000000
17#define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
18
Daniel Hellstrom207e6952008-03-28 10:00:33 +010019DECLARE_GLOBAL_DATA_PTR;
20
Daniel Hellstrom207e6952008-03-28 10:00:33 +010021/*
22 * Breath some life into the CPU...
23 *
24 * Set up the memory map,
25 * initialize a bunch of registers.
26 *
27 * Run from FLASH/PROM:
Loïc Minier5d0569a2011-02-03 22:04:26 +010028 * - until memory controller is set up, only registers available
Daniel Hellstrom207e6952008-03-28 10:00:33 +010029 * - no global variables available for writing
Loïc Minier5d0569a2011-02-03 22:04:26 +010030 * - constants available
Daniel Hellstrom207e6952008-03-28 10:00:33 +010031 */
32
33void cpu_init_f(void)
34{
35 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
36
37 /* initialize the IRQMP */
38 leon2->Interrupt_Force = 0;
39 leon2->Interrupt_Pending = 0;
40 leon2->Interrupt_Clear = 0xfffe; /* clear all old pending interrupts */
41 leon2->Interrupt_Mask = 0xfffe0000; /* mask all IRQs */
42
43 /* cache */
44
Francois Retief21ffc1f2015-11-21 17:07:48 +020045 /* I/O port setup */
Daniel Hellstrom207e6952008-03-28 10:00:33 +010046#ifdef LEON2_IO_PORT_DIR
Francois Retief21ffc1f2015-11-21 17:07:48 +020047 leon2->PIO_Direction = LEON2_IO_PORT_DIR;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010048#endif
49#ifdef LEON2_IO_PORT_DATA
Francois Retief21ffc1f2015-11-21 17:07:48 +020050 leon2->PIO_Data = LEON2_IO_PORT_DATA;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010051#endif
52#ifdef LEON2_IO_PORT_INT
Francois Retief21ffc1f2015-11-21 17:07:48 +020053 leon2->PIO_Interrupt = LEON2_IO_PORT_INT;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010054#else
Francois Retief21ffc1f2015-11-21 17:07:48 +020055 leon2->PIO_Interrupt = 0;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010056#endif
57}
58
59void cpu_init_f2(void)
60{
Francois Retiefe3051d92015-10-28 14:29:32 +020061
62}
63
64int arch_cpu_init(void)
65{
66 gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
67 gd->bus_clk = CONFIG_SYS_CLK_FREQ;
68 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010069
Francois Retiefe3051d92015-10-28 14:29:32 +020070 return 0;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010071}
72
73/*
74 * initialize higher level parts of CPU like time base and timers
75 */
76int cpu_init_r(void)
77{
78 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
79
80 /* initialize prescaler common to all timers to 1MHz */
81 leon2->Scaler_Counter = leon2->Scaler_Reload =
82 (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
83
84 return (0);
85}
86
87/* Uses Timer 0 to get accurate
88 * pauses. Max 2 raised to 32 ticks
89 *
90 */
91void cpu_wait_ticks(unsigned long ticks)
92{
93 unsigned long start = get_timer(0);
94 while (get_timer(start) < ticks) ;
95}
96
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +020097/* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
Daniel Hellstrom207e6952008-03-28 10:00:33 +010098 * Return irq number for timer int or a negative number for
99 * dealing with self
100 */
101int timer_interrupt_init_cpu(void)
102{
103 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
104
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200105 /* SYS_HZ ticks per second */
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100106 leon2->Timer_Counter_1 = 0;
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200107 leon2->Timer_Reload_1 = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100108 leon2->Timer_Control_1 =
109 (LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS | LEON2_TIMER_CTRL_LD);
110
111 return LEON2_TIMER1_IRQNO;
112}
113
Daniel Hellstrom9c2a0f22014-05-08 18:52:37 +0200114ulong get_tbclk(void)
115{
116 return TIMER_BASE_CLK;
117}
118
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100119/*
120 * This function is intended for SHORT delays only.
121 */
122unsigned long cpu_usec2ticks(unsigned long usec)
123{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200124 if (usec < US_PER_TICK)
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100125 return 1;
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200126 return usec / US_PER_TICK;
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100127}
128
129unsigned long cpu_ticks2usec(unsigned long ticks)
130{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200131 return ticks * US_PER_TICK;
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100132}