f23ecb89be73f4b82814ea103ab6d6b69cfc145d
[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 <ambapp.h>
14
15 #include <config.h>
16
17 /* Default Plug&Play I/O area */
18 #ifndef CONFIG_AMBAPP_IOAREA
19 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
20 #endif
21
22 #define TIMER_BASE_CLK 1000000
23 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 /* reset CPU (jump to 0, without reset) */
28 void start(void);
29
30 /* find & initialize the memory controller */
31 int init_memory_ctrl(void);
32
33 ambapp_dev_irqmp *irqmp = NULL;
34 ambapp_dev_mctrl memctrl;
35 ambapp_dev_gptimer *gptimer = NULL;
36 unsigned int gptimer_irq = 0;
37 int leon3_snooping_avail = 0;
38
39 struct {
40         gd_t gd_area;
41         bd_t bd;
42 } global_data;
43
44 /*
45  * Breath some life into the CPU...
46  *
47  * Run from FLASH/PROM:
48  *  - until memory controller is set up, only registers available
49  *  - memory controller has already been setup up, stack can be used
50  *  - no global variables available for writing
51  *  - constants available
52  */
53 void cpu_init_f(void)
54 {
55
56 }
57
58 /* Routine called from start.S,
59  *
60  * Run from FLASH/PROM:
61  *  - memory controller has already been setup up, stack can be used
62  *  - global variables available for read/writing
63  *  - constants avaiable
64          */
65 void cpu_init_f2(void)
66 {
67         /* Initialize the AMBA Plug & Play bus structure, the bus
68          * structure represents the AMBA bus that the CPU is located at.
69          */
70         ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
71 }
72
73 /*
74  * initialize higher level parts of CPU like time base and timers
75  */
76 int cpu_init_r(void)
77 {
78         ambapp_apbdev apbdev;
79         int index, cpu;
80         ambapp_dev_gptimer *timer = NULL;
81         unsigned int bus_freq;
82
83         /*
84          * Find AMBA APB IRQMP Controller,
85          */
86         if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
87                 GAISLER_IRQMP, 0, &apbdev) != 1) {
88                 panic("%s: IRQ controller not found\n", __func__);
89                 return -1;
90         }
91         irqmp = (ambapp_dev_irqmp *)apbdev.address;
92
93         /* initialize the IRQMP */
94         irqmp->ilevel = 0xf;    /* all IRQ off */
95         irqmp->iforce = 0;
96         irqmp->ipend = 0;
97         irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
98         for (cpu = 0; cpu < 16; cpu++) {
99                 /* mask and clear force for all IRQs on CPU[N] */
100                 irqmp->cpu_mask[cpu] = 0;
101                 irqmp->cpu_force[cpu] = 0;
102         }
103
104         /* timer */
105         index = 0;
106         while (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
107                 index, &apbdev) == 1) {
108                 timer = (ambapp_dev_gptimer *)apbdev.address;
109                 if (gptimer == NULL) {
110                         gptimer = timer;
111                         gptimer_irq = apbdev.irq;
112                 }
113
114                 /* Different buses may have different frequency, the
115                  * frequency of the bus tell in which frequency the timer
116                  * prescaler operates.
117                  */
118                 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
119
120                 /* initialize prescaler common to all timers to 1MHz */
121                 timer->scalar = timer->scalar_reload =
122                     (((bus_freq / 1000) + 500) / 1000) - 1;
123
124                 index++;
125         }
126         if (!gptimer) {
127                 printf("%s: gptimer not found!\n", __func__);
128                 return 1;
129         }
130         return 0;
131 }
132
133 /* Uses Timer 0 to get accurate
134  * pauses. Max 2 raised to 32 ticks
135  *
136  */
137 void cpu_wait_ticks(unsigned long ticks)
138 {
139         unsigned long start = get_timer(0);
140         while (get_timer(start) < ticks) ;
141 }
142
143 /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
144  * Return irq number for timer int or a negative number for
145  * dealing with self
146  */
147 int timer_interrupt_init_cpu(void)
148 {
149         /* SYS_HZ ticks per second */
150         gptimer->e[0].val = 0;
151         gptimer->e[0].rld = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
152         gptimer->e[0].ctrl =
153             (LEON3_GPTIMER_EN |
154              LEON3_GPTIMER_RL | LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
155
156         return gptimer_irq;
157 }
158
159 ulong get_tbclk(void)
160 {
161         return TIMER_BASE_CLK;
162 }
163
164 /*
165  * This function is intended for SHORT delays only.
166  */
167 unsigned long cpu_usec2ticks(unsigned long usec)
168 {
169         if (usec < US_PER_TICK)
170                 return 1;
171         return usec / US_PER_TICK;
172 }
173
174 unsigned long cpu_ticks2usec(unsigned long ticks)
175 {
176         return ticks * US_PER_TICK;
177 }