[0aee2be5] | 1 | /* timer.c |
---|
| 2 | * |
---|
[8430205] | 3 | * This file manages the interval timer on the PowerPC MPC5xx. |
---|
| 4 | * NOTE: This is not the PIT, but rather the RTEMS interval |
---|
| 5 | * timer |
---|
| 6 | * We shall use the bottom 32 bits of the timebase register, |
---|
[0aee2be5] | 7 | * |
---|
[8430205] | 8 | * The following was in the 403 version of this file. I don't |
---|
| 9 | * know what it means. JTM 5/19/98 |
---|
[0aee2be5] | 10 | * NOTE: It is important that the timer start/stop overhead be |
---|
| 11 | * determined when porting or modifying this code. |
---|
| 12 | * |
---|
[8430205] | 13 | * |
---|
| 14 | * MPC5xx port sponsored by Defence Research and Development Canada - Suffield |
---|
| 15 | * Copyright (C) 2004, Real-Time Systems Inc. (querbach@realtime.bc.ca) |
---|
| 16 | * |
---|
| 17 | * Derived from c/src/lib/libcpu/powerpc/mpc8xx/timer/timer.c: |
---|
| 18 | * |
---|
| 19 | * Author: Jay Monkman (jmonkman@frasca.com) |
---|
| 20 | * Copywright (C) 1998 by Frasca International, Inc. |
---|
| 21 | * |
---|
| 22 | * Derived from c/src/lib/libcpu/ppc/ppc403/timer/timer.c: |
---|
| 23 | * |
---|
| 24 | * Author: Andrew Bray <andy@i-cubed.co.uk> |
---|
| 25 | * |
---|
| 26 | * COPYRIGHT (c) 1995 by i-cubed ltd. |
---|
| 27 | * |
---|
| 28 | * To anyone who acknowledges that this file is provided "AS IS" |
---|
| 29 | * without any express or implied warranty: |
---|
| 30 | * permission to use, copy, modify, and distribute this file |
---|
| 31 | * for any purpose is hereby granted without fee, provided that |
---|
| 32 | * the above copyright notice and this notice appears in all |
---|
| 33 | * copies, and that the name of i-cubed limited not be used in |
---|
| 34 | * advertising or publicity pertaining to distribution of the |
---|
| 35 | * software without specific, written prior permission. |
---|
| 36 | * i-cubed limited makes no representations about the suitability |
---|
| 37 | * of this software for any purpose. |
---|
| 38 | * |
---|
| 39 | * Derived from c/src/lib/libcpu/hppa1_1/timer/timer.c: |
---|
| 40 | * |
---|
[c4cc8199] | 41 | * COPYRIGHT (c) 1989-2007. |
---|
[0aee2be5] | 42 | * On-Line Applications Research Corporation (OAR). |
---|
| 43 | * |
---|
[8430205] | 44 | * The license and distribution terms for this file may be |
---|
| 45 | * found in the file LICENSE in this distribution or at |
---|
| 46 | * http://www.rtems.com/license/LICENSE. |
---|
[0aee2be5] | 47 | */ |
---|
| 48 | |
---|
| 49 | #include <rtems.h> |
---|
[8430205] | 50 | #include <mpc5xx.h> |
---|
| 51 | |
---|
[ad17f7f] | 52 | static volatile uint32_t Timer_starting; |
---|
[3942cce] | 53 | static bool benchmark_timer_find_average_overhead; |
---|
[8430205] | 54 | |
---|
[14a78df] | 55 | extern uint32_t bsp_timer_least_valid; |
---|
| 56 | extern uint32_t bsp_timer_average_overhead; |
---|
| 57 | |
---|
[8430205] | 58 | /* |
---|
| 59 | * This is so small that this code will be reproduced where needed. |
---|
| 60 | */ |
---|
[ad17f7f] | 61 | static inline uint32_t get_itimer(void) |
---|
[8430205] | 62 | { |
---|
[ad17f7f] | 63 | uint32_t ret; |
---|
[0aee2be5] | 64 | |
---|
[f9acc33] | 65 | __asm__ volatile ("mftb %0" : "=r" ((ret))); /* TBLO */ |
---|
[0aee2be5] | 66 | |
---|
[8430205] | 67 | return ret; |
---|
| 68 | } |
---|
[0aee2be5] | 69 | |
---|
[6427f1a] | 70 | void benchmark_timer_initialize(void) |
---|
[0aee2be5] | 71 | { |
---|
[8430205] | 72 | /* set interrupt level and enable timebase. This should never */ |
---|
| 73 | /* generate an interrupt however. */ |
---|
| 74 | usiu.tbscrk = USIU_UNLOCK_KEY; |
---|
| 75 | usiu.tbscr |= USIU_TBSCR_TBIRQ(4) /* interrupt priority level */ |
---|
| 76 | | USIU_TBSCR_TBF /* freeze timebase during debug */ |
---|
| 77 | | USIU_TBSCR_TBE; /* enable timebase */ |
---|
| 78 | usiu.tbscrk = 0; |
---|
[359e537] | 79 | |
---|
[8430205] | 80 | Timer_starting = get_itimer(); |
---|
[0aee2be5] | 81 | } |
---|
| 82 | |
---|
[6427f1a] | 83 | int benchmark_timer_read(void) |
---|
[0aee2be5] | 84 | { |
---|
[ad17f7f] | 85 | uint32_t clicks; |
---|
| 86 | uint32_t total; |
---|
[0aee2be5] | 87 | |
---|
[8430205] | 88 | clicks = get_itimer(); |
---|
| 89 | |
---|
| 90 | total = clicks - Timer_starting; |
---|
| 91 | |
---|
[6427f1a] | 92 | if ( benchmark_timer_find_average_overhead == 1 ) |
---|
[8430205] | 93 | return total; /* in XXX microsecond units */ |
---|
| 94 | |
---|
| 95 | else { |
---|
[c4cc8199] | 96 | if ( total < bsp_timer_least_valid ) { |
---|
[8430205] | 97 | return 0; /* below timer resolution */ |
---|
| 98 | } |
---|
[c4cc8199] | 99 | return (total - bsp_timer_average_overhead); |
---|
[8430205] | 100 | } |
---|
| 101 | } |
---|
[0aee2be5] | 102 | |
---|
[3942cce] | 103 | void benchmark_timer_disable_subtracting_average_overhead(bool find_flag) |
---|
[0aee2be5] | 104 | { |
---|
[6427f1a] | 105 | benchmark_timer_find_average_overhead = find_flag; |
---|
[0aee2be5] | 106 | } |
---|