sparc: Update cpu_init.c to use generic timer infrastructure
[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 #define TIMER_BASE_CLK 1000000
32 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 ambapp_dev_irqmp *irqmp = NULL;
37
38 /*
39  * Breath some life into the CPU...
40  *
41  * Run from FLASH/PROM:
42  *  - until memory controller is set up, only registers available
43  *  - memory controller has already been setup up, stack can be used
44  *  - no global variables available for writing
45  *  - constants available
46  */
47 void cpu_init_f(void)
48 {
49 #ifdef CONFIG_DEBUG_UART
50         debug_uart_init();
51 #endif
52 }
53
54 /* If cache snooping is available in hardware the result will be set
55  * to 0x800000, otherwise 0.
56  */
57 static unsigned int snoop_detect(void)
58 {
59         unsigned int result;
60         asm("lda [%%g0] 2, %0" : "=r"(result));
61         return result & 0x00800000;
62 }
63
64 int arch_cpu_init(void)
65 {
66         ambapp_apbdev apbdev;
67         int index;
68
69         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
70         gd->bus_clk = CONFIG_SYS_CLK_FREQ;
71         gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
72
73         gd->arch.snooping_available = snoop_detect();
74
75         /* Initialize the AMBA Plug & Play bus structure, the bus
76          * structure represents the AMBA bus that the CPU is located at.
77          */
78         ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
79
80         /* Initialize/clear all the timers in the system.
81          */
82         for (index = 0; ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
83                 GAISLER_GPTIMER, index, &apbdev) == 1; index++) {
84                 ambapp_dev_gptimer *timer;
85                 unsigned int bus_freq;
86                 int i, ntimers;
87
88                 timer = (ambapp_dev_gptimer *)apbdev.address;
89
90                 /* Different buses may have different frequency, the
91                  * frequency of the bus tell in which frequency the timer
92                  * prescaler operates.
93                  */
94                 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
95
96                 /* Initialize prescaler common to all timers to 1MHz */
97                 timer->scalar = timer->scalar_reload =
98                         (((bus_freq / 1000) + 500) / 1000) - 1;
99
100                 /* Clear all timers */
101                 ntimers = timer->config & 0x7;
102                 for (i = 0; i < ntimers; i++) {
103                         timer->e[i].ctrl = GPTIMER_CTRL_IP;
104                         timer->e[i].rld = 0;
105                         timer->e[i].ctrl = GPTIMER_CTRL_LD;
106                 }
107         }
108
109         return 0;
110 }
111
112 /*
113  * initialize higher level parts of CPU like time base and timers
114  */
115 int cpu_init_r(void)
116 {
117         ambapp_apbdev apbdev;
118         int cpu;
119
120         /*
121          * Find AMBA APB IRQMP Controller,
122          */
123         if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
124                 GAISLER_IRQMP, 0, &apbdev) != 1) {
125                 panic("%s: IRQ controller not found\n", __func__);
126                 return -1;
127         }
128         irqmp = (ambapp_dev_irqmp *)apbdev.address;
129
130         /* initialize the IRQMP */
131         irqmp->ilevel = 0xf;    /* all IRQ off */
132         irqmp->iforce = 0;
133         irqmp->ipend = 0;
134         irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
135         for (cpu = 0; cpu < 16; cpu++) {
136                 /* mask and clear force for all IRQs on CPU[N] */
137                 irqmp->cpu_mask[cpu] = 0;
138                 irqmp->cpu_force[cpu] = 0;
139         }
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 int timer_interrupt_init_cpu(void)
155 {
156         return -1;
157 }
158
159 /*
160  * This function is intended for SHORT delays only.
161  */
162 unsigned long cpu_usec2ticks(unsigned long usec)
163 {
164         if (usec < US_PER_TICK)
165                 return 1;
166         return usec / US_PER_TICK;
167 }
168
169 unsigned long cpu_ticks2usec(unsigned long ticks)
170 {
171         return ticks * US_PER_TICK;
172 }
173
174 int timer_init(void)
175 {
176         ambapp_dev_gptimer_element *tmr;
177         ambapp_dev_gptimer *gptimer;
178         ambapp_apbdev apbdev;
179         unsigned bus_freq;
180
181         if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
182                 CONFIG_SYS_GRLIB_GPTIMER_INDEX, &apbdev) != 1) {
183                 panic("%s: gptimer not found!\n", __func__);
184                 return -1;
185         }
186
187         gptimer = (ambapp_dev_gptimer *) apbdev.address;
188
189         /* Different buses may have different frequency, the
190          * frequency of the bus tell in which frequency the timer
191          * prescaler operates.
192          */
193         bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
194
195         /* initialize prescaler common to all timers to 1MHz */
196         gptimer->scalar = gptimer->scalar_reload =
197                 (((bus_freq / 1000) + 500) / 1000) - 1;
198
199         tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
200
201         tmr->val = 0;
202         tmr->rld = ~0;
203         tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
204
205         CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;
206         return 0;
207 }