]> git.ipfire.org Git - people/ms/u-boot.git/blob - arch/sparc/cpu/leon3/cpu_init.c
9e294bfd0ebb3eacf83a999babc4045d73daa5e4
[people/ms/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 <asm/io.h>
14 #include <asm/irq.h>
15 #include <ambapp.h>
16 #include <grlib/irqmp.h>
17 #include <grlib/gptimer.h>
18 #include <debug_uart.h>
19
20 #include <config.h>
21
22 /* Default Plug&Play I/O area */
23 #ifndef CONFIG_AMBAPP_IOAREA
24 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
25 #endif
26
27 /* Select which TIMER that will become the time base */
28 #ifndef CONFIG_SYS_GRLIB_GPTIMER_INDEX
29 #define CONFIG_SYS_GRLIB_GPTIMER_INDEX 0
30 #endif
31
32 #define TIMER_BASE_CLK 1000000
33 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 ambapp_dev_irqmp *irqmp = NULL;
38
39 /*
40 * Breath some life into the CPU...
41 *
42 * Run from FLASH/PROM:
43 * - until memory controller is set up, only registers available
44 * - memory controller has already been setup up, stack can be used
45 * - no global variables available for writing
46 * - constants available
47 */
48 void cpu_init_f(void)
49 {
50 #ifdef CONFIG_DEBUG_UART
51 debug_uart_init();
52 #endif
53 }
54
55 /* If cache snooping is available in hardware the result will be set
56 * to 0x800000, otherwise 0.
57 */
58 static unsigned int snoop_detect(void)
59 {
60 unsigned int result;
61 asm("lda [%%g0] 2, %0" : "=r"(result));
62 return result & 0x00800000;
63 }
64
65 int arch_cpu_init(void)
66 {
67 ambapp_apbdev apbdev;
68 int index;
69
70 gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
71 gd->bus_clk = CONFIG_SYS_CLK_FREQ;
72 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
73
74 gd->arch.snooping_available = snoop_detect();
75
76 /* Initialize the AMBA Plug & Play bus structure, the bus
77 * structure represents the AMBA bus that the CPU is located at.
78 */
79 ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
80
81 /* Initialize/clear all the timers in the system.
82 */
83 for (index = 0; ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
84 GAISLER_GPTIMER, index, &apbdev) == 1; index++) {
85 ambapp_dev_gptimer *timer;
86 unsigned int bus_freq;
87 int i, ntimers;
88
89 timer = (ambapp_dev_gptimer *)apbdev.address;
90
91 /* Different buses may have different frequency, the
92 * frequency of the bus tell in which frequency the timer
93 * prescaler operates.
94 */
95 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
96
97 /* Initialize prescaler common to all timers to 1MHz */
98 timer->scalar = timer->scalar_reload =
99 (((bus_freq / 1000) + 500) / 1000) - 1;
100
101 /* Clear all timers */
102 ntimers = timer->config & 0x7;
103 for (i = 0; i < ntimers; i++) {
104 timer->e[i].ctrl = GPTIMER_CTRL_IP;
105 timer->e[i].rld = 0;
106 timer->e[i].ctrl = GPTIMER_CTRL_LD;
107 }
108 }
109
110 return 0;
111 }
112
113 /*
114 * initialize higher level parts of CPU like time base and timers
115 */
116 int cpu_init_r(void)
117 {
118 ambapp_apbdev apbdev;
119 int cpu;
120
121 /*
122 * Find AMBA APB IRQMP Controller,
123 */
124 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
125 GAISLER_IRQMP, 0, &apbdev) != 1) {
126 panic("%s: IRQ controller not found\n", __func__);
127 return -1;
128 }
129 irqmp = (ambapp_dev_irqmp *)apbdev.address;
130
131 /* initialize the IRQMP */
132 irqmp->ilevel = 0xf; /* all IRQ off */
133 irqmp->iforce = 0;
134 irqmp->ipend = 0;
135 irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
136 for (cpu = 0; cpu < 16; cpu++) {
137 /* mask and clear force for all IRQs on CPU[N] */
138 irqmp->cpu_mask[cpu] = 0;
139 irqmp->cpu_force[cpu] = 0;
140 }
141
142 return 0;
143 }
144
145 /* Busy wait a number of ms */
146 void cpu_wait_ms_busy(unsigned long ms)
147 {
148 unsigned int ms_delay;
149 volatile unsigned int tmp;
150
151 /* ~10-20 cycles per decrement */
152 ms_delay = leon_cpu_freq / (1000 * 10);
153 do {
154 /* Wait ~1ms */
155 tmp = ms_delay;
156 while (tmp-- > 0)
157 ;
158 } while (--ms > 0);
159 }
160
161 /* Uses Timer 0 to get accurate
162 * pauses. Max 2 raised to 32 ticks
163 *
164 */
165 void cpu_wait_ticks(unsigned long ticks)
166 {
167 unsigned long start;
168
169 if (interrupt_is_enabled()) {
170 start = get_timer(0);
171 while (get_timer(start) < ticks)
172 ;
173 } else {
174 /* Interrupts disabled, this means that we cannot
175 * use get_timer(), it relies on IRQ. Instead the
176 * CPU frequency is used.
177 */
178 cpu_wait_ms_busy(ticks2usec(ticks) / 1000);
179 }
180 }
181
182 int timer_interrupt_init_cpu(void)
183 {
184 return -1;
185 }
186
187 /*
188 * This function is intended for SHORT delays only.
189 */
190 unsigned long cpu_usec2ticks(unsigned long usec)
191 {
192 if (usec < US_PER_TICK)
193 return 1;
194 return usec / US_PER_TICK;
195 }
196
197 unsigned long cpu_ticks2usec(unsigned long ticks)
198 {
199 return ticks * US_PER_TICK;
200 }
201
202 int timer_init(void)
203 {
204 ambapp_dev_gptimer_element *tmr;
205 ambapp_dev_gptimer *gptimer;
206 ambapp_apbdev apbdev;
207 unsigned bus_freq;
208
209 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
210 CONFIG_SYS_GRLIB_GPTIMER_INDEX, &apbdev) != 1) {
211 panic("%s: gptimer not found!\n", __func__);
212 return -1;
213 }
214
215 gptimer = (ambapp_dev_gptimer *) apbdev.address;
216
217 /* Different buses may have different frequency, the
218 * frequency of the bus tell in which frequency the timer
219 * prescaler operates.
220 */
221 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
222
223 /* initialize prescaler common to all timers to 1MHz */
224 gptimer->scalar = gptimer->scalar_reload =
225 (((bus_freq / 1000) + 500) / 1000) - 1;
226
227 tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
228
229 tmr->val = 0;
230 tmr->rld = ~0;
231 tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
232
233 CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;
234 return 0;
235 }