[4a238002] | 1 | /* |
---|
| 2 | * timer for the Hitachi SH 704X |
---|
| 3 | * |
---|
| 4 | * This file manages the benchmark timer used by the RTEMS Timing Test |
---|
| 5 | * Suite. Each measured time period is demarcated by calls to |
---|
| 6 | * Timer_initialize() and Read_timer(). Read_timer() usually returns |
---|
| 7 | * the number of microseconds since Timer_initialize() exitted. |
---|
| 8 | * |
---|
| 9 | * NOTE: It is important that the timer start/stop overhead be |
---|
| 10 | * determined when porting or modifying this code. |
---|
| 11 | * |
---|
| 12 | * Authors: Ralf Corsepius (corsepiu@faw.uni-ulm.de) and |
---|
| 13 | * Bernd Becker (becker@faw.uni-ulm.de) |
---|
| 14 | * |
---|
| 15 | * COPYRIGHT (c) 1997-1998, FAW Ulm, Germany |
---|
| 16 | * |
---|
| 17 | * This program is distributed in the hope that it will be useful, |
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
| 20 | * |
---|
| 21 | * COPYRIGHT (c) 1998. |
---|
| 22 | * On-Line Applications Research Corporation (OAR). |
---|
| 23 | * |
---|
| 24 | * The license and distribution terms for this file may be |
---|
| 25 | * found in the file LICENSE in this distribution or at |
---|
| 26 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 27 | * |
---|
| 28 | * $Id$ |
---|
| 29 | */ |
---|
| 30 | |
---|
| 31 | #include <rtems.h> |
---|
| 32 | |
---|
| 33 | #include <rtems/score/sh_io.h> |
---|
| 34 | #include <rtems/score/iosh7045.h> |
---|
| 35 | |
---|
| 36 | /* |
---|
| 37 | * We use a Phi/4 timer |
---|
| 38 | */ |
---|
| 39 | #define SCALE (Timer_MHZ/4) |
---|
| 40 | |
---|
| 41 | #define MTU1_STARTMASK 0xfd |
---|
| 42 | #define MTU1_SYNCMASK 0xfd |
---|
| 43 | #define MTU1_MODEMASK 0xc0 |
---|
| 44 | #define MTU1_TCRMASK 0x01 |
---|
| 45 | #define MTU1_TIORMASK 0x88 |
---|
| 46 | #define MTU1_STAT_MASK 0xf8 |
---|
| 47 | #define MTU1_TIERMASK 0xfc |
---|
| 48 | #define IPRC_MTU1_MASK 0xfff0 |
---|
| 49 | |
---|
| 50 | #ifndef MTU1_PRIO |
---|
| 51 | #define MTU1_PRIO 15 |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | #define MTU1_VECTOR 86 |
---|
| 55 | |
---|
| 56 | rtems_isr timerisr(); |
---|
| 57 | |
---|
| 58 | static rtems_unsigned32 Timer_interrupts; |
---|
| 59 | |
---|
| 60 | rtems_boolean Timer_driver_Find_average_overhead; |
---|
| 61 | |
---|
| 62 | static rtems_unsigned32 Timer_MHZ ; |
---|
| 63 | |
---|
| 64 | void Timer_initialize( void ) |
---|
| 65 | { |
---|
| 66 | rtems_unsigned8 temp8; |
---|
| 67 | rtems_unsigned16 temp16; |
---|
| 68 | rtems_unsigned32 level; |
---|
| 69 | rtems_isr *ignored; |
---|
| 70 | |
---|
| 71 | Timer_MHZ = rtems_cpu_configuration_get_clicks_per_second() / 1000000 ; |
---|
| 72 | |
---|
| 73 | /* |
---|
| 74 | * Timer has never overflowed. This may not be necessary on some |
---|
| 75 | * implemenations of timer but .... |
---|
| 76 | */ |
---|
| 77 | |
---|
| 78 | Timer_interrupts /* .i */ = 0; |
---|
| 79 | _CPU_ISR_Disable( level); |
---|
| 80 | |
---|
| 81 | /* |
---|
| 82 | * Somehow start the timer |
---|
| 83 | */ |
---|
| 84 | /* stop Timer 1 */ |
---|
| 85 | temp8 = read8( MTU_TSTR) & MTU1_STARTMASK; |
---|
| 86 | write8( temp8, MTU_TSTR); |
---|
| 87 | |
---|
| 88 | /* initialize counter 1 */ |
---|
| 89 | write16( 0, MTU_TCNT1); |
---|
| 90 | |
---|
| 91 | /* Timer 1 is independent of other timers */ |
---|
| 92 | temp8 = read8( MTU_TSYR) & MTU1_SYNCMASK; |
---|
| 93 | write8( temp8, MTU_TSYR); |
---|
| 94 | |
---|
| 95 | /* Timer 1, normal mode */ |
---|
| 96 | temp8 = read8( MTU_TMDR1) & MTU1_MODEMASK; |
---|
| 97 | write8( temp8, MTU_TMDR1); |
---|
| 98 | |
---|
| 99 | /* x0000000 |
---|
| 100 | * |||||+++--- Internal Clock |
---|
| 101 | * |||++------ Count on rising edge |
---|
| 102 | * |++-------- disable TCNT clear |
---|
| 103 | * +---------- don`t care |
---|
| 104 | */ |
---|
| 105 | write8( MTU1_TCRMASK, MTU_TCR1); |
---|
| 106 | |
---|
| 107 | /* gra and grb are not used */ |
---|
| 108 | write8( MTU1_TIORMASK, MTU_TIOR1); |
---|
| 109 | |
---|
| 110 | /* reset all status flags */ |
---|
| 111 | temp8 = read8( MTU_TSR1) & MTU1_STAT_MASK; |
---|
| 112 | write8( temp8, MTU_TSR1); |
---|
| 113 | |
---|
| 114 | /* enable overflow interrupt */ |
---|
| 115 | write8( MTU1_TIERMASK, MTU_TIER1); |
---|
| 116 | |
---|
| 117 | /* set interrupt priority */ |
---|
| 118 | temp16 = read16( INTC_IPRC) & IPRC_MTU1_MASK; |
---|
| 119 | temp16 |= MTU1_PRIO; |
---|
| 120 | write16( temp16, INTC_IPRC); |
---|
| 121 | |
---|
| 122 | /* initialize ISR */ |
---|
| 123 | _CPU_ISR_install_raw_handler( MTU1_VECTOR, timerisr, &ignored ); |
---|
| 124 | _CPU_ISR_Enable( level); |
---|
| 125 | |
---|
| 126 | /* start timer 1 */ |
---|
| 127 | temp8 = read8( MTU_TSTR) | ~MTU1_STARTMASK; |
---|
| 128 | write8( temp8, MTU_TSTR); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /* |
---|
| 132 | * The following controls the behavior of Read_timer(). |
---|
| 133 | * |
---|
| 134 | * AVG_OVERHEAD is the overhead for starting and stopping the timer. It |
---|
| 135 | * is usually deducted from the number returned. |
---|
| 136 | * |
---|
| 137 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
| 138 | * below this are "noise" and zero is returned. |
---|
| 139 | */ |
---|
| 140 | |
---|
| 141 | #define AVG_OVERHEAD 1 /* It typically takes X.X microseconds */ |
---|
| 142 | /* (Y countdowns) to start/stop the timer. */ |
---|
| 143 | /* This value is in microseconds. */ |
---|
| 144 | #define LEAST_VALID 0 /* 20 */ /* Don't trust a clicks value lower than this */ |
---|
| 145 | |
---|
| 146 | int Read_timer( void ) |
---|
| 147 | { |
---|
| 148 | rtems_unsigned32 clicks; |
---|
| 149 | rtems_unsigned32 total ; |
---|
| 150 | /* |
---|
| 151 | * Read the timer and see how many clicks it has been since we started. |
---|
| 152 | */ |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | clicks = read16( MTU_TCNT1); /* XXX: read some HW here */ |
---|
| 156 | |
---|
| 157 | /* |
---|
| 158 | * Total is calculated by taking into account the number of timer overflow |
---|
| 159 | * interrupts since the timer was initialized and clicks since the last |
---|
| 160 | * interrupts. |
---|
| 161 | */ |
---|
| 162 | |
---|
| 163 | total = clicks + Timer_interrupts * 65536 ; |
---|
| 164 | |
---|
| 165 | if ( Timer_driver_Find_average_overhead ) |
---|
| 166 | return total / SCALE; /* in XXX microsecond units */ |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | if ( total < LEAST_VALID ) |
---|
| 170 | return 0; /* below timer resolution */ |
---|
| 171 | /* |
---|
| 172 | * Somehow convert total into microseconds |
---|
| 173 | */ |
---|
| 174 | return (total / SCALE - AVG_OVERHEAD) ; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* |
---|
| 179 | * Empty function call used in loops to measure basic cost of looping |
---|
| 180 | * in Timing Test Suite. |
---|
| 181 | */ |
---|
| 182 | |
---|
| 183 | rtems_status_code Empty_function( void ) |
---|
| 184 | { |
---|
| 185 | return RTEMS_SUCCESSFUL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void Set_find_average_overhead( |
---|
| 189 | rtems_boolean find_flag |
---|
| 190 | ) |
---|
| 191 | { |
---|
| 192 | Timer_driver_Find_average_overhead = find_flag; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /* Timer 1 is used */ |
---|
| 196 | |
---|
| 197 | #pragma interrupt |
---|
| 198 | void timerisr( void ) |
---|
| 199 | { |
---|
| 200 | unsigned8 temp8; |
---|
| 201 | |
---|
| 202 | /* reset the flags of the status register */ |
---|
| 203 | temp8 = read8( MTU_TSR1) & MTU1_STAT_MASK; |
---|
| 204 | write8( temp8, MTU_TSR1); |
---|
| 205 | |
---|
| 206 | Timer_interrupts += 1; |
---|
| 207 | } |
---|