source: rtems/c/src/lib/libbsp/i386/pc386/clock/ckinit.c @ beefa112

5
Last change on this file since beefa112 was beefa112, checked in by Chris Johns <chrisj@…>, on 05/06/16 at 07:51:26

bsp/pc386: Use irq-generic.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/**
2 *  @file
3 *
4 *  Clock Tick Device Driver
5 *
6 *  History:
7 *    + Original driver was go32 clock by Joel Sherrill
8 *    + go32 clock driver hardware code was inserted into new
9 *      boilerplate when the pc386 BSP by:
10 *        Pedro Miguel Da Cruz Neto Romano <pmcnr@camoes.rnl.ist.utl.pt>
11 *        Jose Rufino <ruf@asterix.ist.utl.pt>
12 *    + Reworked by Joel Sherrill to use clock driver template.
13 *      This removes all boilerplate and leave original hardware
14 *      code I developed for the go32 BSP.
15 */
16
17/*
18 *  COPYRIGHT (c) 1989-2012.
19 *  On-Line Applications Research Corporation (OAR).
20 *
21 *  The license and distribution terms for this file may be
22 *  found in the file LICENSE in this distribution or at
23 *  http://www.rtems.org/license/LICENSE.
24 */
25
26#include <bsp.h>
27#include <bsp/irq-generic.h>
28#include <bspopts.h>
29#include <libcpu/cpuModel.h>
30#include <assert.h>
31#include <rtems/timecounter.h>
32
33#define CLOCK_VECTOR 0
34
35volatile uint32_t pc386_microseconds_per_isr;
36volatile uint32_t pc386_isrs_per_tick;
37uint32_t pc386_clock_click_count;
38
39/* forward declaration */
40void Clock_isr(void *param);
41static void clockOff(void);
42static void Clock_isr_handler(void *param);
43
44/*
45 * Roughly the number of cycles per second. Note that these
46 * will be wildly inaccurate if the chip speed changes due to power saving
47 * or thermal modes.
48 *
49 * NOTE: These are only used when the TSC method is used.
50 */
51static uint64_t pc586_tsc_frequency;
52
53static struct timecounter pc386_tc;
54
55/* this driver may need to count ISRs per tick */
56#define CLOCK_DRIVER_ISRS_PER_TICK       1
57#define CLOCK_DRIVER_ISRS_PER_TICK_VALUE pc386_isrs_per_tick
58
59extern volatile uint32_t Clock_driver_ticks;
60
61#define READ_8254( _lsb, _msb )                               \
62  do { outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_LATCH);      \
63     inport_byte(TIMER_CNTR0, _lsb);                          \
64     inport_byte(TIMER_CNTR0, _msb);                          \
65  } while (0)
66
67
68#ifdef RTEMS_SMP
69#define Clock_driver_support_at_tick() \
70  _SMP_Send_message_broadcast(SMP_MESSAGE_CLOCK_TICK)
71#endif
72
73#define Clock_driver_support_install_isr( _new, _old ) \
74  do { \
75    _old = NULL; \
76  } while(0)
77
78static uint32_t pc386_get_timecount_tsc(struct timecounter *tc)
79{
80  return (uint32_t)rdtsc();
81}
82
83static uint32_t pc386_get_timecount_i8254(struct timecounter *tc)
84{
85  uint32_t                 irqs;
86  uint8_t                  lsb, msb;
87  rtems_interrupt_level    level;
88
89  /*
90   * Fetch all the data in an interrupt critical section.
91   */
92  rtems_interrupt_disable(level);
93    READ_8254(lsb, msb);
94    irqs = Clock_driver_ticks;
95  rtems_interrupt_enable(level);
96
97  return (irqs + 1) * pc386_microseconds_per_isr - ((msb << 8) | lsb);
98}
99
100/*
101 * Calibrate CPU cycles per tick. Interrupts should be disabled.
102 */
103static void calibrate_tsc(void)
104{
105  uint64_t              begin_time;
106  uint8_t               then_lsb, then_msb, now_lsb, now_msb;
107  uint32_t              i;
108
109  /*
110   * We just reset the timer, so we know we're at the beginning of a tick.
111   */
112
113  /*
114   * Count cycles. Watching the timer introduces a several microsecond
115   * uncertaintity, so let it cook for a while and divide by the number of
116   * ticks actually executed.
117   */
118
119  begin_time = rdtsc();
120
121  for (i = rtems_clock_get_ticks_per_second() * pc386_isrs_per_tick;
122       i != 0; --i ) {
123    /* We know we've just completed a tick when timer goes from low to high */
124    then_lsb = then_msb = 0xff;
125    do {
126      READ_8254(now_lsb, now_msb);
127      if ((then_msb < now_msb) ||
128          ((then_msb == now_msb) && (then_lsb < now_lsb)))
129        break;
130      then_lsb = now_lsb;
131      then_msb = now_msb;
132    } while (1);
133  }
134
135  pc586_tsc_frequency = rdtsc() - begin_time;
136
137#if 0
138  printk( "CPU clock at %u MHz\n", (uint32_t)(pc586_tsc_frequency / 1000000));
139#endif
140}
141
142static void clockOn(void)
143{
144  pc386_isrs_per_tick        = 1;
145  pc386_microseconds_per_isr = rtems_configuration_get_microseconds_per_tick();
146
147  while (US_TO_TICK(pc386_microseconds_per_isr) > 65535) {
148    pc386_isrs_per_tick  *= 10;
149    pc386_microseconds_per_isr /= 10;
150  }
151  pc386_clock_click_count = US_TO_TICK(pc386_microseconds_per_isr);
152
153  bsp_interrupt_vector_enable( BSP_PERIODIC_TIMER - BSP_IRQ_VECTOR_BASE );
154
155  #if 0
156    printk( "configured usecs per tick=%d \n",
157      rtems_configuration_get_microseconds_per_tick() );
158    printk( "Microseconds per ISR =%d\n", pc386_microseconds_per_isr );
159    printk( "final ISRs per=%d\n", pc386_isrs_per_tick );
160    printk( "final timer counts=%d\n", pc386_clock_click_count );
161  #endif
162
163  outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN);
164  outport_byte(TIMER_CNTR0, pc386_clock_click_count >> 0 & 0xff);
165  outport_byte(TIMER_CNTR0, pc386_clock_click_count >> 8 & 0xff);
166
167  /*
168   * Now calibrate cycles per tick. Do this every time we
169   * turn the clock on in case the CPU clock speed has changed.
170   */
171  if ( x86_has_tsc() )
172    calibrate_tsc();
173}
174
175static void clockOff(void)
176{
177  /* reset timer mode to standard (BIOS) value */
178  outport_byte(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN);
179  outport_byte(TIMER_CNTR0, 0);
180  outport_byte(TIMER_CNTR0, 0);
181} /* Clock_exit */
182
183bool Clock_isr_enabled = false;
184static void Clock_isr_handler(void *param)
185{
186  if ( Clock_isr_enabled )
187    Clock_isr( param );
188}
189
190void Clock_driver_install_handler(void)
191{
192  rtems_status_code status;
193
194  status = rtems_interrupt_handler_install(
195    BSP_PERIODIC_TIMER,
196    "ckinit",
197    RTEMS_INTERRUPT_UNIQUE,
198    Clock_isr_handler,
199    NULL
200  );
201  assert(status == RTEMS_SUCCESSFUL);
202  clockOn();
203}
204
205#define Clock_driver_support_set_interrupt_affinity(online_processors) \
206  do { \
207    /* FIXME: Is there a way to do this on x86? */ \
208    (void) online_processors; \
209  } while (0)
210
211void Clock_driver_support_initialize_hardware(void)
212{
213  bool use_tsc = false;
214  bool use_8254 = false;
215
216  #if (CLOCK_DRIVER_USE_TSC == 1)
217    use_tsc = true;
218  #endif
219
220  #if (CLOCK_DRIVER_USE_8254 == 1)
221    use_8254 = true;
222  #endif
223
224  if ( !use_tsc && !use_8254 ) {
225    if ( x86_has_tsc() ) use_tsc  = true;
226    else                 use_8254 = true;
227  }
228
229  if ( use_8254 ) {
230    /* printk( "Use 8254\n" ); */
231    pc386_tc.tc_get_timecount = pc386_get_timecount_i8254;
232    pc386_tc.tc_counter_mask = 0xffffffff;
233    pc386_tc.tc_frequency = TIMER_TICK;
234  } else {
235    /* printk( "Use TSC\n" ); */
236    pc386_tc.tc_get_timecount = pc386_get_timecount_tsc;
237    pc386_tc.tc_counter_mask = 0xffffffff;
238    pc386_tc.tc_frequency = pc586_tsc_frequency;
239  }
240
241  pc386_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
242  rtems_timecounter_install(&pc386_tc);
243  Clock_isr_enabled = true;
244}
245
246#define Clock_driver_support_shutdown_hardware() \
247  do { \
248    rtems_status_code status; \
249    clockOff(); \
250    status = rtems_interrupt_handler_remove(  \
251      BSP_PERIODIC_TIMER, \
252      Clock_isr_handler,  \
253      NULL  \
254    );  \
255    assert(status == RTEMS_SUCCESSFUL); \
256  } while (0)
257
258#include "../../../shared/clockdrv_shell.h"
Note: See TracBrowser for help on using the repository browser.