blob: f25388cf841123127c0b9e359afbd8752e65136d [file] [log] [blame]
Daniel Hellstromb552dbe2008-03-26 22:51:29 +01001/* Initializes CPU and basic hardware such as memory
2 * controllers, IRQ controller and system timer 0.
3 *
Daniel Hellstrom02e2a842010-01-25 09:54:51 +01004 * (C) Copyright 2007, 2015
5 * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com
Daniel Hellstromb552dbe2008-03-26 22:51:29 +01006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Daniel Hellstromb552dbe2008-03-26 22:51:29 +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 Hellstromb552dbe2008-03-26 22:51:29 +010014#include <ambapp.h>
Daniel Hellstrom9d59af92010-01-21 16:09:37 +010015#include <grlib/irqmp.h>
16#include <grlib/gptimer.h>
Francois Retiefd18eb7b2015-10-29 12:55:34 +020017#include <debug_uart.h>
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010018
19#include <config.h>
20
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010021/* Default Plug&Play I/O area */
22#ifndef CONFIG_AMBAPP_IOAREA
23#define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
24#endif
25
Francois Retief7d07d682015-10-28 15:18:22 +020026/* 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
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010031DECLARE_GLOBAL_DATA_PTR;
32
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010033ambapp_dev_irqmp *irqmp = NULL;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010034
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010035/*
36 * Breath some life into the CPU...
37 *
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010038 * Run from FLASH/PROM:
Loïc Minier5d0569a2011-02-03 22:04:26 +010039 * - until memory controller is set up, only registers available
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010040 * - memory controller has already been setup up, stack can be used
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010041 * - no global variables available for writing
Loïc Minier5d0569a2011-02-03 22:04:26 +010042 * - constants available
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010043 */
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010044void cpu_init_f(void)
45{
Francois Retiefd18eb7b2015-10-29 12:55:34 +020046#ifdef CONFIG_DEBUG_UART
47 debug_uart_init();
48#endif
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010049}
50
Francois Retiefb1d20d92015-10-28 15:48:41 +020051/* If cache snooping is available in hardware the result will be set
52 * to 0x800000, otherwise 0.
53 */
54static unsigned int snoop_detect(void)
55{
56 unsigned int result;
57 asm("lda [%%g0] 2, %0" : "=r"(result));
58 return result & 0x00800000;
59}
60
Francois Retiefe3051d92015-10-28 14:29:32 +020061int arch_cpu_init(void)
62{
Francois Retief7d07d682015-10-28 15:18:22 +020063 ambapp_apbdev apbdev;
64 int index;
65
Francois Retiefe3051d92015-10-28 14:29:32 +020066 gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
67 gd->bus_clk = CONFIG_SYS_CLK_FREQ;
68 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
69
Francois Retiefb1d20d92015-10-28 15:48:41 +020070 gd->arch.snooping_available = snoop_detect();
71
Francois Retief7cb14212015-10-29 12:58:52 +020072 /* Initialize the AMBA Plug & Play bus structure, the bus
73 * structure represents the AMBA bus that the CPU is located at.
74 */
75 ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
76
Francois Retief7d07d682015-10-28 15:18:22 +020077 /* Initialize/clear all the timers in the system.
78 */
79 for (index = 0; ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
80 GAISLER_GPTIMER, index, &apbdev) == 1; index++) {
81 ambapp_dev_gptimer *timer;
82 unsigned int bus_freq;
83 int i, ntimers;
84
85 timer = (ambapp_dev_gptimer *)apbdev.address;
86
87 /* Different buses may have different frequency, the
88 * frequency of the bus tell in which frequency the timer
89 * prescaler operates.
90 */
91 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
92
93 /* Initialize prescaler common to all timers to 1MHz */
94 timer->scalar = timer->scalar_reload =
95 (((bus_freq / 1000) + 500) / 1000) - 1;
96
97 /* Clear all timers */
98 ntimers = timer->config & 0x7;
99 for (i = 0; i < ntimers; i++) {
100 timer->e[i].ctrl = GPTIMER_CTRL_IP;
101 timer->e[i].rld = 0;
102 timer->e[i].ctrl = GPTIMER_CTRL_LD;
103 }
104 }
105
Francois Retiefe3051d92015-10-28 14:29:32 +0200106 return 0;
107}
108
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100109/*
110 * initialize higher level parts of CPU like time base and timers
111 */
112int cpu_init_r(void)
113{
114 ambapp_apbdev apbdev;
Francois Retief7d07d682015-10-28 15:18:22 +0200115 int cpu;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100116
117 /*
118 * Find AMBA APB IRQMP Controller,
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100119 */
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100120 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
121 GAISLER_IRQMP, 0, &apbdev) != 1) {
122 panic("%s: IRQ controller not found\n", __func__);
123 return -1;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100124 }
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100125 irqmp = (ambapp_dev_irqmp *)apbdev.address;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100126
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100127 /* initialize the IRQMP */
128 irqmp->ilevel = 0xf; /* all IRQ off */
129 irqmp->iforce = 0;
130 irqmp->ipend = 0;
131 irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
132 for (cpu = 0; cpu < 16; cpu++) {
133 /* mask and clear force for all IRQs on CPU[N] */
134 irqmp->cpu_mask[cpu] = 0;
135 irqmp->cpu_force[cpu] = 0;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100136 }
137
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100138 return 0;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100139}
140
Daniel Hellstrom649c68b2010-01-22 12:02:40 +0100141 ;
Francois Retief7d07d682015-10-28 15:18:22 +0200142int timer_init(void)
143{
144 ambapp_dev_gptimer_element *tmr;
145 ambapp_dev_gptimer *gptimer;
146 ambapp_apbdev apbdev;
147 unsigned bus_freq;
148
149 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
150 CONFIG_SYS_GRLIB_GPTIMER_INDEX, &apbdev) != 1) {
151 panic("%s: gptimer not found!\n", __func__);
152 return -1;
153 }
154
155 gptimer = (ambapp_dev_gptimer *) apbdev.address;
156
157 /* Different buses may have different frequency, the
158 * frequency of the bus tell in which frequency the timer
159 * prescaler operates.
160 */
161 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
162
163 /* initialize prescaler common to all timers to 1MHz */
164 gptimer->scalar = gptimer->scalar_reload =
165 (((bus_freq / 1000) + 500) / 1000) - 1;
166
167 tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
168
169 tmr->val = 0;
170 tmr->rld = ~0;
171 tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
172
173 CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;
174 return 0;
175}