Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 1 | /* Initializes CPU and basic hardware such as memory |
| 2 | * controllers, IRQ controller and system timer 0. |
| 3 | * |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 4 | * (C) Copyright 2007, 2015 |
| 5 | * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <asm/asi.h> |
| 12 | #include <asm/leon.h> |
Francois Retief | 7d07d68 | 2015-10-28 15:18:22 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 14 | #include <ambapp.h> |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 15 | #include <grlib/irqmp.h> |
| 16 | #include <grlib/gptimer.h> |
Francois Retief | d18eb7b | 2015-10-29 12:55:34 +0200 | [diff] [blame] | 17 | #include <debug_uart.h> |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 18 | |
| 19 | #include <config.h> |
| 20 | |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 21 | /* Default Plug&Play I/O area */ |
| 22 | #ifndef CONFIG_AMBAPP_IOAREA |
| 23 | #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA |
| 24 | #endif |
| 25 | |
Francois Retief | 7d07d68 | 2015-10-28 15:18:22 +0200 | [diff] [blame] | 26 | /* 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 Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 33 | ambapp_dev_irqmp *irqmp = NULL; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 34 | |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 35 | /* |
| 36 | * Breath some life into the CPU... |
| 37 | * |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 38 | * Run from FLASH/PROM: |
Loïc Minier | 5d0569a | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 39 | * - until memory controller is set up, only registers available |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 40 | * - memory controller has already been setup up, stack can be used |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 41 | * - no global variables available for writing |
Loïc Minier | 5d0569a | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 42 | * - constants available |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 43 | */ |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 44 | void cpu_init_f(void) |
| 45 | { |
Francois Retief | d18eb7b | 2015-10-29 12:55:34 +0200 | [diff] [blame] | 46 | #ifdef CONFIG_DEBUG_UART |
| 47 | debug_uart_init(); |
| 48 | #endif |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Francois Retief | b1d20d9 | 2015-10-28 15:48:41 +0200 | [diff] [blame] | 51 | /* If cache snooping is available in hardware the result will be set |
| 52 | * to 0x800000, otherwise 0. |
| 53 | */ |
| 54 | static 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 Retief | e3051d9 | 2015-10-28 14:29:32 +0200 | [diff] [blame] | 61 | int arch_cpu_init(void) |
| 62 | { |
Francois Retief | 7d07d68 | 2015-10-28 15:18:22 +0200 | [diff] [blame] | 63 | ambapp_apbdev apbdev; |
| 64 | int index; |
| 65 | |
Francois Retief | e3051d9 | 2015-10-28 14:29:32 +0200 | [diff] [blame] | 66 | 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 Retief | b1d20d9 | 2015-10-28 15:48:41 +0200 | [diff] [blame] | 70 | gd->arch.snooping_available = snoop_detect(); |
| 71 | |
Francois Retief | 7cb1421 | 2015-10-29 12:58:52 +0200 | [diff] [blame] | 72 | /* 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 Retief | 7d07d68 | 2015-10-28 15:18:22 +0200 | [diff] [blame] | 77 | /* 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 Retief | e3051d9 | 2015-10-28 14:29:32 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 109 | /* |
| 110 | * initialize higher level parts of CPU like time base and timers |
| 111 | */ |
| 112 | int cpu_init_r(void) |
| 113 | { |
| 114 | ambapp_apbdev apbdev; |
Francois Retief | 7d07d68 | 2015-10-28 15:18:22 +0200 | [diff] [blame] | 115 | int cpu; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * Find AMBA APB IRQMP Controller, |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 119 | */ |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 120 | 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 Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 124 | } |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 125 | irqmp = (ambapp_dev_irqmp *)apbdev.address; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 126 | |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 127 | /* 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 Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 138 | return 0; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Daniel Hellstrom | 649c68b | 2010-01-22 12:02:40 +0100 | [diff] [blame] | 141 | ; |
Francois Retief | 7d07d68 | 2015-10-28 15:18:22 +0200 | [diff] [blame] | 142 | int 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 | } |