1 | /* Clock_initialize |
---|
2 | * |
---|
3 | * This routine initializes the Timer/Counter on the Intel |
---|
4 | * 386ex evaluation board. |
---|
5 | * |
---|
6 | * The tick frequency is 1 millisecond. |
---|
7 | * |
---|
8 | * Input parameters: NONE |
---|
9 | * |
---|
10 | * Output parameters: NONE |
---|
11 | * |
---|
12 | * COPYRIGHT (c) 1989-1999. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.com/license/LICENSE. |
---|
18 | * |
---|
19 | * $Id$ |
---|
20 | */ |
---|
21 | |
---|
22 | #include <bsp.h> |
---|
23 | #include <bsp/irq.h> |
---|
24 | |
---|
25 | #include <stdlib.h> |
---|
26 | |
---|
27 | uint32_t Clock_isrs; /* ISRs until next tick */ |
---|
28 | static uint32_t Clock_initial_isr_value; |
---|
29 | |
---|
30 | volatile uint32_t Clock_driver_ticks; |
---|
31 | |
---|
32 | extern void Clock_exit( void ); |
---|
33 | |
---|
34 | /* External Prototypes */ |
---|
35 | extern void init_rtc(void); /* defined in 'rtc.c' */ |
---|
36 | extern long rtc_read(rtems_time_of_day *); /* defined in 'rtc.c' */ |
---|
37 | |
---|
38 | /* |
---|
39 | * These are set by clock driver during its init |
---|
40 | */ |
---|
41 | |
---|
42 | rtems_device_major_number rtems_clock_major = ~0; |
---|
43 | rtems_device_major_number rtems_clock_minor = 0; |
---|
44 | |
---|
45 | /* |
---|
46 | * This is the ISR handler. |
---|
47 | */ |
---|
48 | |
---|
49 | void Clock_isr(rtems_irq_hdl_param unused) |
---|
50 | { |
---|
51 | /* enable_tracing(); */ |
---|
52 | Clock_driver_ticks += 1; |
---|
53 | if ( Clock_isrs == 1 ) { |
---|
54 | rtems_clock_tick(); |
---|
55 | Clock_isrs = Clock_initial_isr_value; /* rtems_configuration_get_microseconds_per_tick() / 1000;*/ |
---|
56 | } |
---|
57 | else |
---|
58 | Clock_isrs -= 1; |
---|
59 | } |
---|
60 | |
---|
61 | void ClockOff(const rtems_irq_connect_data* unused) |
---|
62 | { |
---|
63 | outport_byte (TIMER_CONFIG, 0x80 ); /* disable the counter timer */ |
---|
64 | } |
---|
65 | |
---|
66 | void ClockOn(const rtems_irq_connect_data* unused) |
---|
67 | { |
---|
68 | outport_byte (TIMER_CONFIG, 0x00 ); /* enable the counter timer */ |
---|
69 | } |
---|
70 | |
---|
71 | int ClockIsOn(const rtems_irq_connect_data* unused) |
---|
72 | { |
---|
73 | return ((i8259s_cache & 0x1) == 0); |
---|
74 | } |
---|
75 | |
---|
76 | static rtems_irq_connect_data clockIrqData = {BSP_PERIODIC_TIMER, |
---|
77 | Clock_isr, |
---|
78 | 0, |
---|
79 | ClockOn, |
---|
80 | ClockOff, |
---|
81 | ClockIsOn}; |
---|
82 | |
---|
83 | rtems_device_driver Clock_initialize( |
---|
84 | rtems_device_major_number major, |
---|
85 | rtems_device_minor_number minor, |
---|
86 | void *pargp |
---|
87 | ) |
---|
88 | { |
---|
89 | unsigned timer_counter_init_value; |
---|
90 | unsigned char clock_lsb, clock_msb; |
---|
91 | |
---|
92 | #ifdef BSP_DEBUG |
---|
93 | printk("Initializing clock driver in Clock_initialize().\n"); |
---|
94 | #endif |
---|
95 | |
---|
96 | #ifdef LOAD_RTC_AT_START |
---|
97 | /* Initialize clock from on-board real time clock. This breaks the */ |
---|
98 | /* test code which assumes which assumes the application will do it. */ |
---|
99 | { |
---|
100 | rtems_time_of_day now; |
---|
101 | |
---|
102 | |
---|
103 | #ifdef BSP_DEBUG |
---|
104 | printk("Loading clock from on-board real-time clock.\n"); |
---|
105 | #endif |
---|
106 | |
---|
107 | init_rtc(); |
---|
108 | if (rtc_read(&now) >= 0) |
---|
109 | rtems_clock_set(&now); |
---|
110 | } |
---|
111 | #endif |
---|
112 | |
---|
113 | Clock_driver_ticks = 0; |
---|
114 | |
---|
115 | Clock_isrs = |
---|
116 | Clock_initial_isr_value = |
---|
117 | rtems_configuration_get_microseconds_per_tick() / 1000; /* ticks per clock_isr */ |
---|
118 | |
---|
119 | /* |
---|
120 | * configure the counter timer ( should be based on microsecs/tick ) |
---|
121 | * NB. The divisor(Clock_isrs) resolves the is the same number that appears in confdefs.h |
---|
122 | * when setting the microseconds_per_tick value. |
---|
123 | */ |
---|
124 | ClockOff ( &clockIrqData ); |
---|
125 | |
---|
126 | timer_counter_init_value = rtems_configuration_get_microseconds_per_tick() / Clock_isrs; |
---|
127 | clock_lsb = (unsigned char)timer_counter_init_value; |
---|
128 | clock_msb = timer_counter_init_value >> 8; |
---|
129 | |
---|
130 | outport_byte (TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN); |
---|
131 | outport_byte (TIMER_CNTR0, clock_lsb ); /* load LSB first */ |
---|
132 | outport_byte (TIMER_CNTR0, clock_msb ); /* then MSB */ |
---|
133 | |
---|
134 | if (!BSP_install_rtems_irq_handler (&clockIrqData)) { |
---|
135 | printk("Unable to initialize system clock\n"); |
---|
136 | rtems_fatal_error_occurred(1); |
---|
137 | } |
---|
138 | |
---|
139 | /* |
---|
140 | * make major/minor avail to others such as shared memory driver |
---|
141 | */ |
---|
142 | |
---|
143 | rtems_clock_major = major; |
---|
144 | rtems_clock_minor = minor; |
---|
145 | |
---|
146 | return RTEMS_SUCCESSFUL; |
---|
147 | } |
---|
148 | |
---|
149 | void Clock_exit(void) |
---|
150 | { |
---|
151 | ClockOff(&clockIrqData); |
---|
152 | BSP_remove_rtems_irq_handler (&clockIrqData); |
---|
153 | } |
---|