f25388cf841123127c0b9e359afbd8752e65136d
[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 <asm/io.h>
14 #include <ambapp.h>
15 #include <grlib/irqmp.h>
16 #include <grlib/gptimer.h>
17 #include <debug_uart.h>
18
19 #include <config.h>
20
21 /* Default Plug&Play I/O area */
22 #ifndef CONFIG_AMBAPP_IOAREA
23 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
24 #endif
25
26 /* Select which TIMER that will become the time base */
27 #ifndef CONFIG_SYS_GRLIB_GPTIMER_INDEX
28 #define CONFIG_SYS_GRLIB_GPTIMER_INDEX 0
29 #endif
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 ambapp_dev_irqmp *irqmp = NULL;
34
35 /*
36  * Breath some life into the CPU...
37  *
38  * Run from FLASH/PROM:
39  *  - until memory controller is set up, only registers available
40  *  - memory controller has already been setup up, stack can be used
41  *  - no global variables available for writing
42  *  - constants available
43  */
44 void cpu_init_f(void)
45 {
46 #ifdef CONFIG_DEBUG_UART
47         debug_uart_init();
48 #endif
49 }
50
51 /* If cache snooping is available in hardware the result will be set
52  * to 0x800000, otherwise 0.
53  */
54 static unsigned int snoop_detect(void)
55 {
56         unsigned int result;
57         asm("lda [%%g0] 2, %0" : "=r"(result));
58         return result & 0x00800000;
59 }
60
61 int arch_cpu_init(void)
62 {
63         ambapp_apbdev apbdev;
64         int index;
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;
69
70         gd->arch.snooping_available = snoop_detect();
71
72         /* Initialize the AMBA Plug & Play bus structure, the bus
73          * structure represents the AMBA bus that the CPU is located at.
74          */
75         ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
76
77         /* Initialize/clear all the timers in the system.
78          */
79         for (index = 0; ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
80                 GAISLER_GPTIMER, index, &apbdev) == 1; index++) {
81                 ambapp_dev_gptimer *timer;
82                 unsigned int bus_freq;
83                 int i, ntimers;
84
85                 timer = (ambapp_dev_gptimer *)apbdev.address;
86
87                 /* Different buses may have different frequency, the
88                  * frequency of the bus tell in which frequency the timer
89                  * prescaler operates.
90                  */
91                 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
92
93                 /* Initialize prescaler common to all timers to 1MHz */
94                 timer->scalar = timer->scalar_reload =
95                         (((bus_freq / 1000) + 500) / 1000) - 1;
96
97                 /* Clear all timers */
98                 ntimers = timer->config & 0x7;
99                 for (i = 0; i < ntimers; i++) {
100                         timer->e[i].ctrl = GPTIMER_CTRL_IP;
101                         timer->e[i].rld = 0;
102                         timer->e[i].ctrl = GPTIMER_CTRL_LD;
103                 }
104         }
105
106         return 0;
107 }
108
109 /*
110  * initialize higher level parts of CPU like time base and timers
111  */
112 int cpu_init_r(void)
113 {
114         ambapp_apbdev apbdev;
115         int cpu;
116
117         /*
118          * Find AMBA APB IRQMP Controller,
119          */
120         if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
121                 GAISLER_IRQMP, 0, &apbdev) != 1) {
122                 panic("%s: IRQ controller not found\n", __func__);
123                 return -1;
124         }
125         irqmp = (ambapp_dev_irqmp *)apbdev.address;
126
127         /* initialize the IRQMP */
128         irqmp->ilevel = 0xf;    /* all IRQ off */
129         irqmp->iforce = 0;
130         irqmp->ipend = 0;
131         irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
132         for (cpu = 0; cpu < 16; cpu++) {
133                 /* mask and clear force for all IRQs on CPU[N] */
134                 irqmp->cpu_mask[cpu] = 0;
135                 irqmp->cpu_force[cpu] = 0;
136         }
137
138         return 0;
139 }
140
141                         ;
142 int timer_init(void)
143 {
144         ambapp_dev_gptimer_element *tmr;
145         ambapp_dev_gptimer *gptimer;
146         ambapp_apbdev apbdev;
147         unsigned bus_freq;
148
149         if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
150                 CONFIG_SYS_GRLIB_GPTIMER_INDEX, &apbdev) != 1) {
151                 panic("%s: gptimer not found!\n", __func__);
152                 return -1;
153         }
154
155         gptimer = (ambapp_dev_gptimer *) apbdev.address;
156
157         /* Different buses may have different frequency, the
158          * frequency of the bus tell in which frequency the timer
159          * prescaler operates.
160          */
161         bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
162
163         /* initialize prescaler common to all timers to 1MHz */
164         gptimer->scalar = gptimer->scalar_reload =
165                 (((bus_freq / 1000) + 500) / 1000) - 1;
166
167         tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
168
169         tmr->val = 0;
170         tmr->rld = ~0;
171         tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
172
173         CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;
174         return 0;
175 }