blob: b140da31b13bfe6077464190a514eb0a3e81ecae [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>
13#include <ambapp.h>
Daniel Hellstrom9d59af92010-01-21 16:09:37 +010014#include <grlib/irqmp.h>
15#include <grlib/gptimer.h>
Francois Retiefd18eb7b2015-10-29 12:55:34 +020016#include <debug_uart.h>
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010017
18#include <config.h>
19
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010020/* Default Plug&Play I/O area */
21#ifndef CONFIG_AMBAPP_IOAREA
22#define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
23#endif
24
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +020025#define TIMER_BASE_CLK 1000000
26#define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
27
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010028DECLARE_GLOBAL_DATA_PTR;
29
30/* reset CPU (jump to 0, without reset) */
31void start(void);
32
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010033ambapp_dev_irqmp *irqmp = NULL;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010034ambapp_dev_gptimer *gptimer = NULL;
35unsigned int gptimer_irq = 0;
36int leon3_snooping_avail = 0;
37
38struct {
39 gd_t gd_area;
40 bd_t bd;
41} global_data;
42
43/*
44 * Breath some life into the CPU...
45 *
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010046 * Run from FLASH/PROM:
Loïc Minier5d0569a2011-02-03 22:04:26 +010047 * - until memory controller is set up, only registers available
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010048 * - memory controller has already been setup up, stack can be used
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010049 * - no global variables available for writing
Loïc Minier5d0569a2011-02-03 22:04:26 +010050 * - constants available
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010051 */
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010052void cpu_init_f(void)
53{
Francois Retiefd18eb7b2015-10-29 12:55:34 +020054#ifdef CONFIG_DEBUG_UART
55 debug_uart_init();
56#endif
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010057}
58
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010059/* Routine called from start.S,
60 *
61 * Run from FLASH/PROM:
62 * - memory controller has already been setup up, stack can be used
63 * - global variables available for read/writing
64 * - constants avaiable
65 */
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010066void cpu_init_f2(void)
67{
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010068 /* Initialize the AMBA Plug & Play bus structure, the bus
69 * structure represents the AMBA bus that the CPU is located at.
70 */
71 ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010072}
73
74/*
75 * initialize higher level parts of CPU like time base and timers
76 */
77int cpu_init_r(void)
78{
79 ambapp_apbdev apbdev;
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010080 int index, cpu;
81 ambapp_dev_gptimer *timer = NULL;
82 unsigned int bus_freq;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010083
84 /*
85 * Find AMBA APB IRQMP Controller,
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010086 */
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010087 if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
88 GAISLER_IRQMP, 0, &apbdev) != 1) {
89 panic("%s: IRQ controller not found\n", __func__);
90 return -1;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010091 }
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010092 irqmp = (ambapp_dev_irqmp *)apbdev.address;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010093
Daniel Hellstrom02e2a842010-01-25 09:54:51 +010094 /* initialize the IRQMP */
95 irqmp->ilevel = 0xf; /* all IRQ off */
96 irqmp->iforce = 0;
97 irqmp->ipend = 0;
98 irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
99 for (cpu = 0; cpu < 16; cpu++) {
100 /* mask and clear force for all IRQs on CPU[N] */
101 irqmp->cpu_mask[cpu] = 0;
102 irqmp->cpu_force[cpu] = 0;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100103 }
104
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100105 /* timer */
106 index = 0;
107 while (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
108 index, &apbdev) == 1) {
109 timer = (ambapp_dev_gptimer *)apbdev.address;
110 if (gptimer == NULL) {
111 gptimer = timer;
112 gptimer_irq = apbdev.irq;
113 }
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100114
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100115 /* Different buses may have different frequency, the
116 * frequency of the bus tell in which frequency the timer
117 * prescaler operates.
118 */
119 bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100120
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100121 /* initialize prescaler common to all timers to 1MHz */
122 timer->scalar = timer->scalar_reload =
123 (((bus_freq / 1000) + 500) / 1000) - 1;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100124
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100125 index++;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100126 }
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100127 if (!gptimer) {
128 printf("%s: gptimer not found!\n", __func__);
129 return 1;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100130 }
Daniel Hellstrom02e2a842010-01-25 09:54:51 +0100131 return 0;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100132}
133
134/* Uses Timer 0 to get accurate
135 * pauses. Max 2 raised to 32 ticks
136 *
137 */
138void cpu_wait_ticks(unsigned long ticks)
139{
140 unsigned long start = get_timer(0);
141 while (get_timer(start) < ticks) ;
142}
143
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200144/* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100145 * Return irq number for timer int or a negative number for
146 * dealing with self
147 */
148int timer_interrupt_init_cpu(void)
149{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200150 /* SYS_HZ ticks per second */
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100151 gptimer->e[0].val = 0;
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200152 gptimer->e[0].rld = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100153 gptimer->e[0].ctrl =
Daniel Hellstrom9d59af92010-01-21 16:09:37 +0100154 (GPTIMER_CTRL_EN | GPTIMER_CTRL_RS |
155 GPTIMER_CTRL_LD | GPTIMER_CTRL_IE);
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100156
157 return gptimer_irq;
158}
159
Daniel Hellstrom9c2a0f22014-05-08 18:52:37 +0200160ulong get_tbclk(void)
161{
162 return TIMER_BASE_CLK;
163}
164
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100165/*
166 * This function is intended for SHORT delays only.
167 */
168unsigned long cpu_usec2ticks(unsigned long usec)
169{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200170 if (usec < US_PER_TICK)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100171 return 1;
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200172 return usec / US_PER_TICK;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100173}
174
175unsigned long cpu_ticks2usec(unsigned long ticks)
176{
Daniel Hellstrom8f7557d2014-05-08 19:16:14 +0200177 return ticks * US_PER_TICK;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100178}