sparc: Update cpu_init.c to use generic timer infrastructure
authorFrancois Retief <fgretief@spaceteq.co.za>
Wed, 28 Oct 2015 13:18:22 +0000 (15:18 +0200)
committerFrancois Retief <fgretief@spaceteq.co.za>
Thu, 3 Dec 2015 11:15:49 +0000 (13:15 +0200)
Introduce the CONFIG_SYS_TIMER_* macros in include/asm/config.h to make use
of the generic timer infrastructure in lib/time.c.

Created a timer_init() function to initialize the timer hardware and update
the #ifdef in board_init_f to allow this function to be called during the
start-up sequence.

Signed-off-by: Francois Retief <fgretief@spaceteq.co.za>
arch/sparc/cpu/leon2/cpu_init.c
arch/sparc/cpu/leon3/cpu_init.c
arch/sparc/include/asm/config.h
arch/sparc/include/asm/global_data.h
arch/sparc/lib/interrupts.c
common/board_f.c

index 5630b095d49fcd2af22a05001aa3bfa1340a2000..b4d91e50398bde2cc434ea28a995d926a3e4f7c0 100644 (file)
@@ -10,6 +10,7 @@
 #include <common.h>
 #include <asm/asi.h>
 #include <asm/leon.h>
+#include <asm/io.h>
 
 #include <config.h>
 
@@ -54,6 +55,9 @@ void cpu_init_f(void)
 #else
        leon2->PIO_Interrupt = 0;
 #endif
+
+       /* disable timers */
+       leon2->Timer_Control_1 = leon2->Timer_Control_2 = 0;
 }
 
 int arch_cpu_init(void)
@@ -66,17 +70,11 @@ int arch_cpu_init(void)
 }
 
 /*
- * initialize higher level parts of CPU like time base and timers
+ * initialize higher level parts of CPU
  */
 int cpu_init_r(void)
 {
-       LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
-
-       /* initialize prescaler common to all timers to 1MHz */
-       leon2->Scaler_Counter = leon2->Scaler_Reload =
-           (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
-
-       return (0);
+       return 0;
 }
 
 /* Uses Timer 0 to get accurate
@@ -106,11 +104,6 @@ int timer_interrupt_init_cpu(void)
        return LEON2_TIMER1_IRQNO;
 }
 
-ulong get_tbclk(void)
-{
-       return TIMER_BASE_CLK;
-}
-
 /*
  * This function is intended for SHORT delays only.
  */
@@ -125,3 +118,21 @@ unsigned long cpu_ticks2usec(unsigned long ticks)
 {
        return ticks * US_PER_TICK;
 }
+
+int timer_init(void)
+{
+       LEON2_regs *leon2 = (LEON2_regs *)LEON2_PREGS;
+
+       /* initialize prescaler common to all timers to 1MHz */
+       leon2->Scaler_Counter = leon2->Scaler_Reload =
+               (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
+
+       /* SYS_HZ ticks per second */
+       leon2->Timer_Counter_1 = 0;
+       leon2->Timer_Reload_1 = (CONFIG_SYS_TIMER_RATE / CONFIG_SYS_HZ) - 1;
+       leon2->Timer_Control_1 = LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS |
+               LEON2_TIMER_CTRL_LD;
+
+       CONFIG_SYS_TIMER_COUNTER = (void *)&leon2->Timer_Counter_1;
+       return 0;
+}
index 9c7665783cb27930fa2ce0c06e93c131cb9878e9..88f82c95a8bee3d1d0f4baecc8552ba51fae5f17 100644 (file)
@@ -10,6 +10,7 @@
 #include <common.h>
 #include <asm/asi.h>
 #include <asm/leon.h>
+#include <asm/io.h>
 #include <ambapp.h>
 #include <grlib/irqmp.h>
 #include <grlib/gptimer.h>
 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
 #endif
 
+/* Select which TIMER that will become the time base */
+#ifndef CONFIG_SYS_GRLIB_GPTIMER_INDEX
+#define CONFIG_SYS_GRLIB_GPTIMER_INDEX 0
+#endif
+
 #define TIMER_BASE_CLK 1000000
 #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
 
 DECLARE_GLOBAL_DATA_PTR;
 
 ambapp_dev_irqmp *irqmp = NULL;
-ambapp_dev_gptimer *gptimer = NULL;
-unsigned int gptimer_irq = 0;
 
 /*
  * Breath some life into the CPU...
@@ -59,6 +63,9 @@ static unsigned int snoop_detect(void)
 
 int arch_cpu_init(void)
 {
+       ambapp_apbdev apbdev;
+       int index;
+
        gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
        gd->bus_clk = CONFIG_SYS_CLK_FREQ;
        gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
@@ -70,6 +77,35 @@ int arch_cpu_init(void)
         */
        ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
 
+       /* Initialize/clear all the timers in the system.
+        */
+       for (index = 0; ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
+               GAISLER_GPTIMER, index, &apbdev) == 1; index++) {
+               ambapp_dev_gptimer *timer;
+               unsigned int bus_freq;
+               int i, ntimers;
+
+               timer = (ambapp_dev_gptimer *)apbdev.address;
+
+               /* Different buses may have different frequency, the
+                * frequency of the bus tell in which frequency the timer
+                * prescaler operates.
+                */
+               bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
+
+               /* Initialize prescaler common to all timers to 1MHz */
+               timer->scalar = timer->scalar_reload =
+                       (((bus_freq / 1000) + 500) / 1000) - 1;
+
+               /* Clear all timers */
+               ntimers = timer->config & 0x7;
+               for (i = 0; i < ntimers; i++) {
+                       timer->e[i].ctrl = GPTIMER_CTRL_IP;
+                       timer->e[i].rld = 0;
+                       timer->e[i].ctrl = GPTIMER_CTRL_LD;
+               }
+       }
+
        return 0;
 }
 
@@ -79,9 +115,7 @@ int arch_cpu_init(void)
 int cpu_init_r(void)
 {
        ambapp_apbdev apbdev;
-       int index, cpu, ntimers, i;
-       ambapp_dev_gptimer *timer = NULL;
-       unsigned int bus_freq;
+       int cpu;
 
        /*
         * Find AMBA APB IRQMP Controller,
@@ -104,40 +138,6 @@ int cpu_init_r(void)
                irqmp->cpu_force[cpu] = 0;
        }
 
-       /* timer */
-       index = 0;
-       while (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
-               index, &apbdev) == 1) {
-               timer = (ambapp_dev_gptimer *)apbdev.address;
-               if (gptimer == NULL) {
-                       gptimer = timer;
-                       gptimer_irq = apbdev.irq;
-               }
-
-               /* Different buses may have different frequency, the
-                * frequency of the bus tell in which frequency the timer
-                * prescaler operates.
-                */
-               bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
-
-               /* initialize prescaler common to all timers to 1MHz */
-               timer->scalar = timer->scalar_reload =
-                   (((bus_freq / 1000) + 500) / 1000) - 1;
-
-               /* Clear All Timers */
-               ntimers = timer->config & 0x7;
-               for (i = 0; i < ntimers; i++) {
-                       timer->e[i].ctrl = GPTIMER_CTRL_IP;
-                       timer->e[i].rld = 0;
-                       timer->e[i].ctrl = GPTIMER_CTRL_LD;
-               }
-
-               index++;
-       }
-       if (!gptimer) {
-               printf("%s: gptimer not found!\n", __func__);
-               return 1;
-       }
        return 0;
 }
 
@@ -151,25 +151,9 @@ void cpu_wait_ticks(unsigned long ticks)
        while (get_timer(start) < ticks) ;
 }
 
-/* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
- * Return irq number for timer int or a negative number for
- * dealing with self
- */
 int timer_interrupt_init_cpu(void)
 {
-       /* SYS_HZ ticks per second */
-       gptimer->e[0].val = 0;
-       gptimer->e[0].rld = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
-       gptimer->e[0].ctrl =
-           (GPTIMER_CTRL_EN | GPTIMER_CTRL_RS |
-            GPTIMER_CTRL_LD | GPTIMER_CTRL_IE);
-
-       return gptimer_irq;
-}
-
-ulong get_tbclk(void)
-{
-       return TIMER_BASE_CLK;
+       return -1;
 }
 
 /*
@@ -186,3 +170,38 @@ unsigned long cpu_ticks2usec(unsigned long ticks)
 {
        return ticks * US_PER_TICK;
 }
+
+int timer_init(void)
+{
+       ambapp_dev_gptimer_element *tmr;
+       ambapp_dev_gptimer *gptimer;
+       ambapp_apbdev apbdev;
+       unsigned bus_freq;
+
+       if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
+               CONFIG_SYS_GRLIB_GPTIMER_INDEX, &apbdev) != 1) {
+               panic("%s: gptimer not found!\n", __func__);
+               return -1;
+       }
+
+       gptimer = (ambapp_dev_gptimer *) apbdev.address;
+
+       /* Different buses may have different frequency, the
+        * frequency of the bus tell in which frequency the timer
+        * prescaler operates.
+        */
+       bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
+
+       /* initialize prescaler common to all timers to 1MHz */
+       gptimer->scalar = gptimer->scalar_reload =
+               (((bus_freq / 1000) + 500) / 1000) - 1;
+
+       tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
+
+       tmr->val = 0;
+       tmr->rld = ~0;
+       tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
+
+       CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;
+       return 0;
+}
index c884b250fe61586748f8770d7052a7de66359656..455fbc1b73293cffef24af05faeeb362f46c48a8 100644 (file)
@@ -14,4 +14,8 @@
 #define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
+#define CONFIG_SYS_TIMER_RATE          1000000         /* 1MHz */
+#define CONFIG_SYS_TIMER_COUNTER       gd->arch.timer
+#define CONFIG_SYS_TIMER_COUNTS_DOWN
+
 #endif
index 8c6557ad1f40fc62daa3660b3c96c61f0dda9586..af38d17b0dd3f328f89a8e365d3e3fa6e33638c7 100644 (file)
@@ -15,6 +15,7 @@
 
 /* Architecture-specific global data */
 struct arch_global_data {
+       void *timer;
        void *uart;
        unsigned int uart_freq;
 #ifdef CONFIG_LEON3
index b8f2efb303bd148ae1b75b0600f93253ccde9fa3..fab26c62dd6d12959917077ca9298498e2b13c18 100644 (file)
@@ -81,11 +81,6 @@ void timer_interrupt(struct pt_regs *regs)
        timestamp++;
 }
 
-ulong get_timer(ulong base)
-{
-       return (timestamp - base);
-}
-
 void timer_interrupt_init(void)
 {
        int irq;
index b035c90ff3b7a7fd05c1e4b1f9d832b7faf8dba3..2dd10b9819c8899ce0df2e6ca7d0ba7566851599 100644 (file)
@@ -808,7 +808,8 @@ static init_fnc_t init_sequence_f[] = {
        init_timebase,
 #endif
 #if defined(CONFIG_X86) || defined(CONFIG_ARM) || defined(CONFIG_MIPS) || \
-               defined(CONFIG_BLACKFIN) || defined(CONFIG_NDS32)
+               defined(CONFIG_BLACKFIN) || defined(CONFIG_NDS32) || \
+               defined(CONFIG_SPARC)
        timer_init,             /* initialize timer */
 #endif
 #ifdef CONFIG_SYS_ALLOC_DPRAM