Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 1 | /* Initializes CPU and basic hardware such as memory |
| 2 | * controllers, IRQ controller and system timer 0. |
| 3 | * |
Francois Retief | e3051d9 | 2015-10-28 14:29:32 +0200 | [diff] [blame^] | 4 | * (C) Copyright 2007, 2015 |
| 5 | * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +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 | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <asm/asi.h> |
| 12 | #include <asm/leon.h> |
| 13 | |
| 14 | #include <config.h> |
| 15 | |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 16 | #define TIMER_BASE_CLK 1000000 |
| 17 | #define US_PER_TICK (1000000 / CONFIG_SYS_HZ) |
| 18 | |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 21 | /* |
| 22 | * Breath some life into the CPU... |
| 23 | * |
| 24 | * Set up the memory map, |
| 25 | * initialize a bunch of registers. |
| 26 | * |
| 27 | * Run from FLASH/PROM: |
Loïc Minier | 5d0569a | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 28 | * - until memory controller is set up, only registers available |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 29 | * - no global variables available for writing |
Loïc Minier | 5d0569a | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 30 | * - constants available |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | void cpu_init_f(void) |
| 34 | { |
| 35 | LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS; |
| 36 | |
| 37 | /* initialize the IRQMP */ |
| 38 | leon2->Interrupt_Force = 0; |
| 39 | leon2->Interrupt_Pending = 0; |
| 40 | leon2->Interrupt_Clear = 0xfffe; /* clear all old pending interrupts */ |
| 41 | leon2->Interrupt_Mask = 0xfffe0000; /* mask all IRQs */ |
| 42 | |
| 43 | /* cache */ |
| 44 | |
Francois Retief | 21ffc1f | 2015-11-21 17:07:48 +0200 | [diff] [blame] | 45 | /* I/O port setup */ |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 46 | #ifdef LEON2_IO_PORT_DIR |
Francois Retief | 21ffc1f | 2015-11-21 17:07:48 +0200 | [diff] [blame] | 47 | leon2->PIO_Direction = LEON2_IO_PORT_DIR; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 48 | #endif |
| 49 | #ifdef LEON2_IO_PORT_DATA |
Francois Retief | 21ffc1f | 2015-11-21 17:07:48 +0200 | [diff] [blame] | 50 | leon2->PIO_Data = LEON2_IO_PORT_DATA; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 51 | #endif |
| 52 | #ifdef LEON2_IO_PORT_INT |
Francois Retief | 21ffc1f | 2015-11-21 17:07:48 +0200 | [diff] [blame] | 53 | leon2->PIO_Interrupt = LEON2_IO_PORT_INT; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 54 | #else |
Francois Retief | 21ffc1f | 2015-11-21 17:07:48 +0200 | [diff] [blame] | 55 | leon2->PIO_Interrupt = 0; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 56 | #endif |
| 57 | } |
| 58 | |
| 59 | void cpu_init_f2(void) |
| 60 | { |
Francois Retief | e3051d9 | 2015-10-28 14:29:32 +0200 | [diff] [blame^] | 61 | |
| 62 | } |
| 63 | |
| 64 | int arch_cpu_init(void) |
| 65 | { |
| 66 | gd->cpu_clk = CONFIG_SYS_CLK_FREQ; |
| 67 | gd->bus_clk = CONFIG_SYS_CLK_FREQ; |
| 68 | gd->ram_size = CONFIG_SYS_SDRAM_SIZE; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 69 | |
Francois Retief | e3051d9 | 2015-10-28 14:29:32 +0200 | [diff] [blame^] | 70 | return 0; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * initialize higher level parts of CPU like time base and timers |
| 75 | */ |
| 76 | int cpu_init_r(void) |
| 77 | { |
| 78 | LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS; |
| 79 | |
| 80 | /* initialize prescaler common to all timers to 1MHz */ |
| 81 | leon2->Scaler_Counter = leon2->Scaler_Reload = |
| 82 | (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1; |
| 83 | |
| 84 | return (0); |
| 85 | } |
| 86 | |
| 87 | /* Uses Timer 0 to get accurate |
| 88 | * pauses. Max 2 raised to 32 ticks |
| 89 | * |
| 90 | */ |
| 91 | void cpu_wait_ticks(unsigned long ticks) |
| 92 | { |
| 93 | unsigned long start = get_timer(0); |
| 94 | while (get_timer(start) < ticks) ; |
| 95 | } |
| 96 | |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 97 | /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz. |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 98 | * Return irq number for timer int or a negative number for |
| 99 | * dealing with self |
| 100 | */ |
| 101 | int timer_interrupt_init_cpu(void) |
| 102 | { |
| 103 | LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS; |
| 104 | |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 105 | /* SYS_HZ ticks per second */ |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 106 | leon2->Timer_Counter_1 = 0; |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 107 | leon2->Timer_Reload_1 = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 108 | leon2->Timer_Control_1 = |
| 109 | (LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS | LEON2_TIMER_CTRL_LD); |
| 110 | |
| 111 | return LEON2_TIMER1_IRQNO; |
| 112 | } |
| 113 | |
Daniel Hellstrom | 9c2a0f2 | 2014-05-08 18:52:37 +0200 | [diff] [blame] | 114 | ulong get_tbclk(void) |
| 115 | { |
| 116 | return TIMER_BASE_CLK; |
| 117 | } |
| 118 | |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 119 | /* |
| 120 | * This function is intended for SHORT delays only. |
| 121 | */ |
| 122 | unsigned long cpu_usec2ticks(unsigned long usec) |
| 123 | { |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 124 | if (usec < US_PER_TICK) |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 125 | return 1; |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 126 | return usec / US_PER_TICK; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | unsigned long cpu_ticks2usec(unsigned long ticks) |
| 130 | { |
Daniel Hellstrom | 8f7557d | 2014-05-08 19:16:14 +0200 | [diff] [blame] | 131 | return ticks * US_PER_TICK; |
Daniel Hellstrom | 207e695 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 132 | } |