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+
14 #include <grlib/irqmp.h>
15 #include <grlib/gptimer.h>
16 #include <debug_uart.h>
20 /* Default Plug&Play I/O area */
21 #ifndef CONFIG_AMBAPP_IOAREA
22 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
25 #define TIMER_BASE_CLK 1000000
26 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
28 DECLARE_GLOBAL_DATA_PTR;
30 ambapp_dev_irqmp *irqmp = NULL;
31 ambapp_dev_gptimer *gptimer = NULL;
32 unsigned int gptimer_irq = 0;
35 * Breath some life into the CPU...
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
45 #ifdef CONFIG_DEBUG_UART
50 /* Routine called from start.S,
52 * Run from FLASH/PROM:
53 * - memory controller has already been setup up, stack can be used
54 * - global variables available for read/writing
55 * - constants avaiable
57 void cpu_init_f2(void)
61 /* If cache snooping is available in hardware the result will be set
62 * to 0x800000, otherwise 0.
64 static unsigned int snoop_detect(void)
67 asm("lda [%%g0] 2, %0" : "=r"(result));
68 return result & 0x00800000;
71 int arch_cpu_init(void)
73 gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
74 gd->bus_clk = CONFIG_SYS_CLK_FREQ;
75 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
77 gd->arch.snooping_available = snoop_detect();
79 /* Initialize the AMBA Plug & Play bus structure, the bus
80 * structure represents the AMBA bus that the CPU is located at.
82 ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
88 * initialize higher level parts of CPU like time base and timers
94 ambapp_dev_gptimer *timer = NULL;
95 unsigned int bus_freq;
98 * Find AMBA APB IRQMP Controller,
100 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
101 GAISLER_IRQMP, 0, &apbdev) != 1) {
102 panic("%s: IRQ controller not found\n", __func__);
105 irqmp = (ambapp_dev_irqmp *)apbdev.address;
107 /* initialize the IRQMP */
108 irqmp->ilevel = 0xf; /* all IRQ off */
111 irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
112 for (cpu = 0; cpu < 16; cpu++) {
113 /* mask and clear force for all IRQs on CPU[N] */
114 irqmp->cpu_mask[cpu] = 0;
115 irqmp->cpu_force[cpu] = 0;
120 while (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
121 index, &apbdev) == 1) {
122 timer = (ambapp_dev_gptimer *)apbdev.address;
123 if (gptimer == NULL) {
125 gptimer_irq = apbdev.irq;
128 /* Different buses may have different frequency, the
129 * frequency of the bus tell in which frequency the timer
130 * prescaler operates.
132 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
134 /* initialize prescaler common to all timers to 1MHz */
135 timer->scalar = timer->scalar_reload =
136 (((bus_freq / 1000) + 500) / 1000) - 1;
141 printf("%s: gptimer not found!\n", __func__);
147 /* Uses Timer 0 to get accurate
148 * pauses. Max 2 raised to 32 ticks
151 void cpu_wait_ticks(unsigned long ticks)
153 unsigned long start = get_timer(0);
154 while (get_timer(start) < ticks) ;
157 /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
158 * Return irq number for timer int or a negative number for
161 int timer_interrupt_init_cpu(void)
163 /* SYS_HZ ticks per second */
164 gptimer->e[0].val = 0;
165 gptimer->e[0].rld = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
167 (GPTIMER_CTRL_EN | GPTIMER_CTRL_RS |
168 GPTIMER_CTRL_LD | GPTIMER_CTRL_IE);
173 ulong get_tbclk(void)
175 return TIMER_BASE_CLK;
179 * This function is intended for SHORT delays only.
181 unsigned long cpu_usec2ticks(unsigned long usec)
183 if (usec < US_PER_TICK)
185 return usec / US_PER_TICK;
188 unsigned long cpu_ticks2usec(unsigned long ticks)
190 return ticks * US_PER_TICK;