1 /* Initializes CPU and basic hardware such as memory
2 * controllers, IRQ controller and system timer 0.
4 * (C) Copyright 2007, 2015
5 * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <grlib/irqmp.h>
16 #include <grlib/gptimer.h>
17 #include <debug_uart.h>
21 /* Default Plug&Play I/O area */
22 #ifndef CONFIG_AMBAPP_IOAREA
23 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
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
31 #define TIMER_BASE_CLK 1000000
32 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
34 DECLARE_GLOBAL_DATA_PTR;
36 ambapp_dev_irqmp *irqmp = NULL;
39 * Breath some life into the CPU...
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
49 #ifdef CONFIG_DEBUG_UART
54 /* If cache snooping is available in hardware the result will be set
55 * to 0x800000, otherwise 0.
57 static unsigned int snoop_detect(void)
60 asm("lda [%%g0] 2, %0" : "=r"(result));
61 return result & 0x00800000;
64 int arch_cpu_init(void)
69 gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
70 gd->bus_clk = CONFIG_SYS_CLK_FREQ;
71 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
73 gd->arch.snooping_available = snoop_detect();
75 /* Initialize the AMBA Plug & Play bus structure, the bus
76 * structure represents the AMBA bus that the CPU is located at.
78 ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
80 /* Initialize/clear all the timers in the system.
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;
88 timer = (ambapp_dev_gptimer *)apbdev.address;
90 /* Different buses may have different frequency, the
91 * frequency of the bus tell in which frequency the timer
94 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
96 /* Initialize prescaler common to all timers to 1MHz */
97 timer->scalar = timer->scalar_reload =
98 (((bus_freq / 1000) + 500) / 1000) - 1;
100 /* Clear all timers */
101 ntimers = timer->config & 0x7;
102 for (i = 0; i < ntimers; i++) {
103 timer->e[i].ctrl = GPTIMER_CTRL_IP;
105 timer->e[i].ctrl = GPTIMER_CTRL_LD;
113 * initialize higher level parts of CPU like time base and timers
117 ambapp_apbdev apbdev;
121 * Find AMBA APB IRQMP Controller,
123 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
124 GAISLER_IRQMP, 0, &apbdev) != 1) {
125 panic("%s: IRQ controller not found\n", __func__);
128 irqmp = (ambapp_dev_irqmp *)apbdev.address;
130 /* initialize the IRQMP */
131 irqmp->ilevel = 0xf; /* all IRQ off */
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;
144 /* Uses Timer 0 to get accurate
145 * pauses. Max 2 raised to 32 ticks
148 void cpu_wait_ticks(unsigned long ticks)
150 unsigned long start = get_timer(0);
151 while (get_timer(start) < ticks) ;
154 int timer_interrupt_init_cpu(void)
160 * This function is intended for SHORT delays only.
162 unsigned long cpu_usec2ticks(unsigned long usec)
164 if (usec < US_PER_TICK)
166 return usec / US_PER_TICK;
169 unsigned long cpu_ticks2usec(unsigned long ticks)
171 return ticks * US_PER_TICK;
176 ambapp_dev_gptimer_element *tmr;
177 ambapp_dev_gptimer *gptimer;
178 ambapp_apbdev apbdev;
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__);
187 gptimer = (ambapp_dev_gptimer *) apbdev.address;
189 /* Different buses may have different frequency, the
190 * frequency of the bus tell in which frequency the timer
191 * prescaler operates.
193 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
195 /* initialize prescaler common to all timers to 1MHz */
196 gptimer->scalar = gptimer->scalar_reload =
197 (((bus_freq / 1000) + 500) / 1000) - 1;
199 tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
203 tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
205 CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;