blob: b4d91e50398bde2cc434ea28a995d926a3e4f7c0 [file] [log] [blame]
Daniel Hellstrom207e6952008-03-28 10:00:33 +01001/* Initializes CPU and basic hardware such as memory
2 * controllers, IRQ controller and system timer 0.
3 *
Francois Retiefe3051d92015-10-28 14:29:32 +02004 * (C) Copyright 2007, 2015
5 * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com
Daniel Hellstrom207e6952008-03-28 10:00:33 +01006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Daniel Hellstrom207e6952008-03-28 10:00:33 +01008 */
9
10#include <common.h>
11#include <asm/asi.h>
12#include <asm/leon.h>
Francois Retief7d07d682015-10-28 15:18:22 +020013#include <asm/io.h>
Daniel Hellstrom207e6952008-03-28 10:00:33 +010014
15#include <config.h>
16
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +020017#define TIMER_BASE_CLK 1000000
18#define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
19
Daniel Hellstrom207e6952008-03-28 10:00:33 +010020DECLARE_GLOBAL_DATA_PTR;
21
Daniel Hellstrom207e6952008-03-28 10:00:33 +010022/*
23 * Breath some life into the CPU...
24 *
25 * Set up the memory map,
26 * initialize a bunch of registers.
27 *
28 * Run from FLASH/PROM:
Loïc Minier5d0569a2011-02-03 22:04:26 +010029 * - until memory controller is set up, only registers available
Daniel Hellstrom207e6952008-03-28 10:00:33 +010030 * - no global variables available for writing
Loïc Minier5d0569a2011-02-03 22:04:26 +010031 * - constants available
Daniel Hellstrom207e6952008-03-28 10:00:33 +010032 */
33
34void cpu_init_f(void)
35{
36 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
37
38 /* initialize the IRQMP */
39 leon2->Interrupt_Force = 0;
40 leon2->Interrupt_Pending = 0;
41 leon2->Interrupt_Clear = 0xfffe; /* clear all old pending interrupts */
42 leon2->Interrupt_Mask = 0xfffe0000; /* mask all IRQs */
43
44 /* cache */
45
Francois Retief21ffc1f2015-11-21 17:07:48 +020046 /* I/O port setup */
Daniel Hellstrom207e6952008-03-28 10:00:33 +010047#ifdef LEON2_IO_PORT_DIR
Francois Retief21ffc1f2015-11-21 17:07:48 +020048 leon2->PIO_Direction = LEON2_IO_PORT_DIR;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010049#endif
50#ifdef LEON2_IO_PORT_DATA
Francois Retief21ffc1f2015-11-21 17:07:48 +020051 leon2->PIO_Data = LEON2_IO_PORT_DATA;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010052#endif
53#ifdef LEON2_IO_PORT_INT
Francois Retief21ffc1f2015-11-21 17:07:48 +020054 leon2->PIO_Interrupt = LEON2_IO_PORT_INT;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010055#else
Francois Retief21ffc1f2015-11-21 17:07:48 +020056 leon2->PIO_Interrupt = 0;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010057#endif
Francois Retief7d07d682015-10-28 15:18:22 +020058
59 /* disable timers */
60 leon2->Timer_Control_1 = leon2->Timer_Control_2 = 0;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010061}
62
Francois Retiefe3051d92015-10-28 14:29:32 +020063int arch_cpu_init(void)
64{
65 gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
66 gd->bus_clk = CONFIG_SYS_CLK_FREQ;
67 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010068
Francois Retiefe3051d92015-10-28 14:29:32 +020069 return 0;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010070}
71
72/*
Francois Retief7d07d682015-10-28 15:18:22 +020073 * initialize higher level parts of CPU
Daniel Hellstrom207e6952008-03-28 10:00:33 +010074 */
75int cpu_init_r(void)
76{
Francois Retief7d07d682015-10-28 15:18:22 +020077 return 0;
Daniel Hellstrom207e6952008-03-28 10:00:33 +010078}
79
80/* Uses Timer 0 to get accurate
81 * pauses. Max 2 raised to 32 ticks
82 *
83 */
84void cpu_wait_ticks(unsigned long ticks)
85{
86 unsigned long start = get_timer(0);
87 while (get_timer(start) < ticks) ;
88}
89
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +020090/* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
Daniel Hellstrom207e6952008-03-28 10:00:33 +010091 * Return irq number for timer int or a negative number for
92 * dealing with self
93 */
94int timer_interrupt_init_cpu(void)
95{
96 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
97
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +020098 /* SYS_HZ ticks per second */
Daniel Hellstrom207e6952008-03-28 10:00:33 +010099 leon2->Timer_Counter_1 = 0;
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200100 leon2->Timer_Reload_1 = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100101 leon2->Timer_Control_1 =
102 (LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS | LEON2_TIMER_CTRL_LD);
103
104 return LEON2_TIMER1_IRQNO;
105}
106
107/*
108 * This function is intended for SHORT delays only.
109 */
110unsigned long cpu_usec2ticks(unsigned long usec)
111{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200112 if (usec < US_PER_TICK)
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100113 return 1;
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200114 return usec / US_PER_TICK;
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100115}
116
117unsigned long cpu_ticks2usec(unsigned long ticks)
118{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200119 return ticks * US_PER_TICK;
Daniel Hellstrom207e6952008-03-28 10:00:33 +0100120}
Francois Retief7d07d682015-10-28 15:18:22 +0200121
122int timer_init(void)
123{
124 LEON2_regs *leon2 = (LEON2_regs *)LEON2_PREGS;
125
126 /* initialize prescaler common to all timers to 1MHz */
127 leon2->Scaler_Counter = leon2->Scaler_Reload =
128 (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
129
130 /* SYS_HZ ticks per second */
131 leon2->Timer_Counter_1 = 0;
132 leon2->Timer_Reload_1 = (CONFIG_SYS_TIMER_RATE / CONFIG_SYS_HZ) - 1;
133 leon2->Timer_Control_1 = LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS |
134 LEON2_TIMER_CTRL_LD;
135
136 CONFIG_SYS_TIMER_COUNTER = (void *)&leon2->Timer_Counter_1;
137 return 0;
138}