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 | #define TMR0 0xF040 |
---|
22 | #define TMR1 0xF041 |
---|
23 | #define TMR2 0xF042 |
---|
24 | #define TMRCON 0xF043 |
---|
25 | #define TMRCFG 0xF834 |
---|
26 | |
---|
27 | #include <bsp.h> |
---|
28 | #include <irq.h> |
---|
29 | |
---|
30 | #include <rtems/libio.h> |
---|
31 | |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | uint32_t Clock_isrs; /* ISRs until next tick */ |
---|
35 | static uint32_t Clock_initial_isr_value; |
---|
36 | |
---|
37 | volatile uint32_t Clock_driver_ticks; |
---|
38 | |
---|
39 | void Clock_exit( void ); |
---|
40 | |
---|
41 | /* |
---|
42 | * These are set by clock driver during its init |
---|
43 | */ |
---|
44 | |
---|
45 | rtems_device_major_number rtems_clock_major = ~0; |
---|
46 | rtems_device_major_number rtems_clock_minor = 0; |
---|
47 | |
---|
48 | /* |
---|
49 | * This is the ISR handler. |
---|
50 | */ |
---|
51 | |
---|
52 | void Clock_isr() |
---|
53 | { |
---|
54 | /* enable_tracing(); */ |
---|
55 | Clock_driver_ticks += 1; |
---|
56 | if ( Clock_isrs == 1 ) { |
---|
57 | rtems_clock_tick(); |
---|
58 | Clock_isrs = Clock_initial_isr_value; /* BSP_Configuration.microseconds_per_tick / 1000;*/ |
---|
59 | } |
---|
60 | else |
---|
61 | Clock_isrs -= 1; |
---|
62 | } |
---|
63 | |
---|
64 | void ClockOff(const rtems_irq_connect_data* unused) |
---|
65 | { |
---|
66 | outport_byte ( TMRCFG , 0x80 ); /* disable the counter timer */ |
---|
67 | } |
---|
68 | |
---|
69 | void ClockOn(const rtems_irq_connect_data* unused) |
---|
70 | { |
---|
71 | outport_byte ( TMRCFG , 0x00 ); /* enable the counter timer */ |
---|
72 | } |
---|
73 | |
---|
74 | int ClockIsOn(const rtems_irq_connect_data* unused) |
---|
75 | { |
---|
76 | return ((i8259s_cache & 0x1) == 0); |
---|
77 | } |
---|
78 | |
---|
79 | static rtems_irq_connect_data clockIrqData = {BSP_PERIODIC_TIMER, |
---|
80 | Clock_isr, |
---|
81 | ClockOn, |
---|
82 | ClockOff, |
---|
83 | ClockIsOn}; |
---|
84 | |
---|
85 | rtems_device_driver Clock_initialize( |
---|
86 | rtems_device_major_number major, |
---|
87 | rtems_device_minor_number minor, |
---|
88 | void *pargp |
---|
89 | ) |
---|
90 | { |
---|
91 | unsigned timer_counter_init_value; |
---|
92 | unsigned char clock_lsb, clock_msb; |
---|
93 | |
---|
94 | Clock_driver_ticks = 0; |
---|
95 | |
---|
96 | Clock_isrs = |
---|
97 | Clock_initial_isr_value = |
---|
98 | BSP_Configuration.microseconds_per_tick / 1000; /* ticks per clock_isr */ |
---|
99 | |
---|
100 | /* |
---|
101 | * configure the counter timer ( should be based on microsecs/tick ) |
---|
102 | * NB. The divisor(Clock_isrs) resolves the is the same number that appears in confdefs.h |
---|
103 | * when setting the microseconds_per_tick value. |
---|
104 | */ |
---|
105 | ClockOff ( &clockIrqData ); |
---|
106 | |
---|
107 | timer_counter_init_value = BSP_Configuration.microseconds_per_tick / Clock_isrs; |
---|
108 | clock_lsb = (unsigned char)timer_counter_init_value; |
---|
109 | clock_msb = timer_counter_init_value >> 8; |
---|
110 | |
---|
111 | outport_byte ( TMRCON , 0x34 ); |
---|
112 | outport_byte ( TMR0 , clock_lsb ); /* load LSB first */ |
---|
113 | outport_byte ( TMR0 , clock_msb ); /* then MSB */ |
---|
114 | |
---|
115 | if (!BSP_install_rtems_irq_handler (&clockIrqData)) { |
---|
116 | printk("Unable to initialize system clock\n"); |
---|
117 | rtems_fatal_error_occurred(1); |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | * make major/minor avail to others such as shared memory driver |
---|
122 | */ |
---|
123 | |
---|
124 | rtems_clock_major = major; |
---|
125 | rtems_clock_minor = minor; |
---|
126 | |
---|
127 | return RTEMS_SUCCESSFUL; |
---|
128 | } |
---|
129 | |
---|
130 | rtems_device_driver Clock_control( |
---|
131 | rtems_device_major_number major, |
---|
132 | rtems_device_minor_number minor, |
---|
133 | void *pargp |
---|
134 | ) |
---|
135 | { |
---|
136 | rtems_libio_ioctl_args_t *args = pargp; |
---|
137 | |
---|
138 | if (args == 0) |
---|
139 | goto done; |
---|
140 | |
---|
141 | /* |
---|
142 | * This is hokey, but until we get a defined interface |
---|
143 | * to do this, it will just be this simple... |
---|
144 | */ |
---|
145 | |
---|
146 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) |
---|
147 | { |
---|
148 | Clock_isr(); |
---|
149 | } |
---|
150 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) |
---|
151 | { |
---|
152 | if (!BSP_install_rtems_irq_handler (&clockIrqData)) { |
---|
153 | printk("Error installing clock interrupt handler!\n"); |
---|
154 | rtems_fatal_error_occurred(1); |
---|
155 | } |
---|
156 | #ifdef DEBUG |
---|
157 | else |
---|
158 | printk("Clock installed AGAIN\n"); |
---|
159 | #endif |
---|
160 | } |
---|
161 | |
---|
162 | done: |
---|
163 | return RTEMS_SUCCESSFUL; |
---|
164 | } |
---|
165 | |
---|
166 | void Clock_exit() |
---|
167 | { |
---|
168 | ClockOff(&clockIrqData); |
---|
169 | BSP_remove_rtems_irq_handler (&clockIrqData); |
---|
170 | } |
---|