sparc: leon2: Updates for generic board initialization
[oweals/u-boot.git] / arch / sparc / cpu / leon3 / 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 #include <ambapp.h>
14 #include <grlib/irqmp.h>
15 #include <grlib/gptimer.h>
16 #include <debug_uart.h>
17
18 #include <config.h>
19
20 /* Default Plug&Play I/O area */
21 #ifndef CONFIG_AMBAPP_IOAREA
22 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
23 #endif
24
25 #define TIMER_BASE_CLK 1000000
26 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 ambapp_dev_irqmp *irqmp = NULL;
31 ambapp_dev_gptimer *gptimer = NULL;
32 unsigned int gptimer_irq = 0;
33
34 /*
35  * Breath some life into the CPU...
36  *
37  * Run from FLASH/PROM:
38  *  - until memory controller is set up, only registers available
39  *  - memory controller has already been setup up, stack can be used
40  *  - no global variables available for writing
41  *  - constants available
42  */
43 void cpu_init_f(void)
44 {
45 #ifdef CONFIG_DEBUG_UART
46         debug_uart_init();
47 #endif
48 }
49
50 /* If cache snooping is available in hardware the result will be set
51  * to 0x800000, otherwise 0.
52  */
53 static unsigned int snoop_detect(void)
54 {
55         unsigned int result;
56         asm("lda [%%g0] 2, %0" : "=r"(result));
57         return result & 0x00800000;
58 }
59
60 int arch_cpu_init(void)
61 {
62         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
63         gd->bus_clk = CONFIG_SYS_CLK_FREQ;
64         gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
65
66         gd->arch.snooping_available = snoop_detect();
67
68         /* Initialize the AMBA Plug & Play bus structure, the bus
69          * structure represents the AMBA bus that the CPU is located at.
70          */
71         ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
72
73         return 0;
74 }
75
76 /*
77  * initialize higher level parts of CPU like time base and timers
78  */
79 int cpu_init_r(void)
80 {
81         ambapp_apbdev apbdev;
82         int index, cpu, ntimers, i;
83         ambapp_dev_gptimer *timer = NULL;
84         unsigned int bus_freq;
85
86         /*
87          * Find AMBA APB IRQMP Controller,
88          */
89         if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
90                 GAISLER_IRQMP, 0, &apbdev) != 1) {
91                 panic("%s: IRQ controller not found\n", __func__);
92                 return -1;
93         }
94         irqmp = (ambapp_dev_irqmp *)apbdev.address;
95
96         /* initialize the IRQMP */
97         irqmp->ilevel = 0xf;    /* all IRQ off */
98         irqmp->iforce = 0;
99         irqmp->ipend = 0;
100         irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
101         for (cpu = 0; cpu < 16; cpu++) {
102                 /* mask and clear force for all IRQs on CPU[N] */
103                 irqmp->cpu_mask[cpu] = 0;
104                 irqmp->cpu_force[cpu] = 0;
105         }
106
107         /* timer */
108         index = 0;
109         while (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
110                 index, &apbdev) == 1) {
111                 timer = (ambapp_dev_gptimer *)apbdev.address;
112                 if (gptimer == NULL) {
113                         gptimer = timer;
114                         gptimer_irq = apbdev.irq;
115                 }
116
117                 /* Different buses may have different frequency, the
118                  * frequency of the bus tell in which frequency the timer
119                  * prescaler operates.
120                  */
121                 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
122
123                 /* initialize prescaler common to all timers to 1MHz */
124                 timer->scalar = timer->scalar_reload =
125                     (((bus_freq / 1000) + 500) / 1000) - 1;
126
127                 /* Clear All Timers */
128                 ntimers = timer->config & 0x7;
129                 for (i = 0; i < ntimers; i++) {
130                         timer->e[i].ctrl = GPTIMER_CTRL_IP;
131                         timer->e[i].rld = 0;
132                         timer->e[i].ctrl = GPTIMER_CTRL_LD;
133                 }
134
135                 index++;
136         }
137         if (!gptimer) {
138                 printf("%s: gptimer not found!\n", __func__);
139                 return 1;
140         }
141         return 0;
142 }
143
144 /* Uses Timer 0 to get accurate
145  * pauses. Max 2 raised to 32 ticks
146  *
147  */
148 void cpu_wait_ticks(unsigned long ticks)
149 {
150         unsigned long start = get_timer(0);
151         while (get_timer(start) < ticks) ;
152 }
153
154 /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
155  * Return irq number for timer int or a negative number for
156  * dealing with self
157  */
158 int timer_interrupt_init_cpu(void)
159 {
160         /* SYS_HZ ticks per second */
161         gptimer->e[0].val = 0;
162         gptimer->e[0].rld = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
163         gptimer->e[0].ctrl =
164             (GPTIMER_CTRL_EN | GPTIMER_CTRL_RS |
165              GPTIMER_CTRL_LD | GPTIMER_CTRL_IE);
166
167         return gptimer_irq;
168 }
169
170 ulong get_tbclk(void)
171 {
172         return TIMER_BASE_CLK;
173 }
174
175 /*
176  * This function is intended for SHORT delays only.
177  */
178 unsigned long cpu_usec2ticks(unsigned long usec)
179 {
180         if (usec < US_PER_TICK)
181                 return 1;
182         return usec / US_PER_TICK;
183 }
184
185 unsigned long cpu_ticks2usec(unsigned long ticks)
186 {
187         return ticks * US_PER_TICK;
188 }