sparc: leon2: Updates for generic board initialization
[platform/kernel/u-boot.git] / arch / sparc / cpu / leon2 / cpu_init.c
1 /* Initializes CPU and basic hardware such as memory
2  * controllers, IRQ controller and system timer 0.
3  *
4  * (C) Copyright 2007, 2015
5  * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <asm/asi.h>
12 #include <asm/leon.h>
13
14 #include <config.h>
15
16 #define TIMER_BASE_CLK 1000000
17 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /*
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:
28  *  - until memory controller is set up, only registers available
29  *  - no global variables available for writing
30  *  - constants available
31  */
32
33 void 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
45         /* I/O port setup */
46 #ifdef LEON2_IO_PORT_DIR
47         leon2->PIO_Direction = LEON2_IO_PORT_DIR;
48 #endif
49 #ifdef LEON2_IO_PORT_DATA
50         leon2->PIO_Data = LEON2_IO_PORT_DATA;
51 #endif
52 #ifdef LEON2_IO_PORT_INT
53         leon2->PIO_Interrupt = LEON2_IO_PORT_INT;
54 #else
55         leon2->PIO_Interrupt = 0;
56 #endif
57 }
58
59 int arch_cpu_init(void)
60 {
61         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
62         gd->bus_clk = CONFIG_SYS_CLK_FREQ;
63         gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
64
65         return 0;
66 }
67
68 /*
69  * initialize higher level parts of CPU like time base and timers
70  */
71 int cpu_init_r(void)
72 {
73         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
74
75         /* initialize prescaler common to all timers to 1MHz */
76         leon2->Scaler_Counter = leon2->Scaler_Reload =
77             (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
78
79         return (0);
80 }
81
82 /* Uses Timer 0 to get accurate
83  * pauses. Max 2 raised to 32 ticks
84  *
85  */
86 void cpu_wait_ticks(unsigned long ticks)
87 {
88         unsigned long start = get_timer(0);
89         while (get_timer(start) < ticks) ;
90 }
91
92 /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
93  * Return irq number for timer int or a negative number for
94  * dealing with self
95  */
96 int timer_interrupt_init_cpu(void)
97 {
98         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
99
100         /* SYS_HZ ticks per second */
101         leon2->Timer_Counter_1 = 0;
102         leon2->Timer_Reload_1 = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
103         leon2->Timer_Control_1 =
104             (LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS | LEON2_TIMER_CTRL_LD);
105
106         return LEON2_TIMER1_IRQNO;
107 }
108
109 ulong get_tbclk(void)
110 {
111         return TIMER_BASE_CLK;
112 }
113
114 /*
115  * This function is intended for SHORT delays only.
116  */
117 unsigned long cpu_usec2ticks(unsigned long usec)
118 {
119         if (usec < US_PER_TICK)
120                 return 1;
121         return usec / US_PER_TICK;
122 }
123
124 unsigned long cpu_ticks2usec(unsigned long ticks)
125 {
126         return ticks * US_PER_TICK;
127 }