1 | /* ckinit.c |
---|
2 | * |
---|
3 | * Clock device driver for Lattice Mico32 (lm32). |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-1999. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | * |
---|
14 | * Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008, |
---|
15 | * Micro-Research Finland Oy |
---|
16 | */ |
---|
17 | |
---|
18 | #include <stdlib.h> |
---|
19 | |
---|
20 | #include <rtems.h> |
---|
21 | #include <bsp.h> |
---|
22 | #include "../include/system_conf.h" |
---|
23 | #include "clock.h" |
---|
24 | |
---|
25 | static inline int clockread(unsigned int reg) |
---|
26 | { |
---|
27 | return *((int*)(TIMER0_BASE_ADDRESS + reg)); |
---|
28 | } |
---|
29 | |
---|
30 | static inline void clockwrite(unsigned int reg, int value) |
---|
31 | { |
---|
32 | *((int*)(TIMER0_BASE_ADDRESS + reg)) = value; |
---|
33 | } |
---|
34 | |
---|
35 | void Clock_exit( void ); |
---|
36 | rtems_isr Clock_isr( rtems_vector_number vector ); |
---|
37 | extern lm32_isr_entry set_vector(rtems_isr_entry handler, |
---|
38 | rtems_vector_number vector, int type); |
---|
39 | |
---|
40 | /* |
---|
41 | * The interrupt vector number associated with the clock tick device |
---|
42 | * driver. |
---|
43 | */ |
---|
44 | |
---|
45 | #define CLOCK_VECTOR ( TIMER0_IRQ ) |
---|
46 | #define CLOCK_IRQMASK ( 1 << CLOCK_VECTOR ) |
---|
47 | |
---|
48 | /* |
---|
49 | * Clock_driver_ticks is a monotonically increasing counter of the |
---|
50 | * number of clock ticks since the driver was initialized. |
---|
51 | */ |
---|
52 | |
---|
53 | volatile uint32_t Clock_driver_ticks; |
---|
54 | |
---|
55 | /* |
---|
56 | * These are set by clock driver during its init |
---|
57 | */ |
---|
58 | |
---|
59 | rtems_device_major_number rtems_clock_major = ~0; |
---|
60 | rtems_device_minor_number rtems_clock_minor; |
---|
61 | |
---|
62 | /* |
---|
63 | * The previous ISR on this clock tick interrupt vector. |
---|
64 | */ |
---|
65 | |
---|
66 | rtems_isr_entry Old_ticker; |
---|
67 | |
---|
68 | void Clock_exit( void ); |
---|
69 | |
---|
70 | /* |
---|
71 | * Isr Handler |
---|
72 | */ |
---|
73 | |
---|
74 | rtems_isr Clock_isr( |
---|
75 | rtems_vector_number vector |
---|
76 | ) |
---|
77 | { |
---|
78 | /* |
---|
79 | * bump the number of clock driver ticks since initialization |
---|
80 | * |
---|
81 | * determine if it is time to announce the passing of tick as configured |
---|
82 | * to RTEMS through the rtems_clock_tick directive |
---|
83 | * |
---|
84 | * perform any timer dependent tasks |
---|
85 | */ |
---|
86 | |
---|
87 | /* Clear overflow flag */ |
---|
88 | clockwrite(LM32_CLOCK_SR, 0); |
---|
89 | lm32_interrupt_ack(CLOCK_IRQMASK); |
---|
90 | |
---|
91 | /* Increment clock ticks */ |
---|
92 | Clock_driver_ticks += 1; |
---|
93 | |
---|
94 | rtems_clock_tick(); |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | * Install_clock |
---|
99 | * |
---|
100 | * Install a clock tick handler and reprograms the chip. This |
---|
101 | * is used to initially establish the clock tick. |
---|
102 | */ |
---|
103 | |
---|
104 | void Install_clock( |
---|
105 | rtems_isr_entry clock_isr |
---|
106 | ) |
---|
107 | { |
---|
108 | /* |
---|
109 | * Initialize the clock tick device driver variables |
---|
110 | */ |
---|
111 | |
---|
112 | Clock_driver_ticks = 0; |
---|
113 | |
---|
114 | Old_ticker = (rtems_isr_entry) set_vector( clock_isr, CLOCK_VECTOR, 1 ); |
---|
115 | /* |
---|
116 | * Hardware specific initialize goes here |
---|
117 | */ |
---|
118 | |
---|
119 | printk("rtems_configuration_get_microseconds_per_tick() %d\n", |
---|
120 | rtems_configuration_get_microseconds_per_tick()); |
---|
121 | printk("PERIOD %d\n", |
---|
122 | (CPU_FREQUENCY / (1000000 / rtems_configuration_get_microseconds_per_tick()))); |
---|
123 | |
---|
124 | /* Set clock period */ |
---|
125 | clockwrite(LM32_CLOCK_PERIOD, |
---|
126 | (CPU_FREQUENCY / |
---|
127 | (1000000 / rtems_configuration_get_microseconds_per_tick()))); |
---|
128 | |
---|
129 | /* Enable clock interrupts and start in continuous mode */ |
---|
130 | clockwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_ITO | |
---|
131 | LM32_CLOCK_CR_CONT | |
---|
132 | LM32_CLOCK_CR_START); |
---|
133 | |
---|
134 | lm32_interrupt_unmask(CLOCK_IRQMASK); |
---|
135 | |
---|
136 | /* |
---|
137 | * Schedule the clock cleanup routine to execute if the application exits. |
---|
138 | */ |
---|
139 | |
---|
140 | atexit( Clock_exit ); |
---|
141 | } |
---|
142 | |
---|
143 | /* |
---|
144 | * Clean up before the application exits |
---|
145 | */ |
---|
146 | |
---|
147 | void Clock_exit( void ) |
---|
148 | { |
---|
149 | /* Disable clock interrupts and stop */ |
---|
150 | |
---|
151 | lm32_interrupt_unmask(CLOCK_IRQMASK); |
---|
152 | clockwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_STOP); |
---|
153 | } |
---|
154 | |
---|
155 | /* |
---|
156 | * Clock_initialize |
---|
157 | * |
---|
158 | * Device driver entry point for clock tick driver initialization. |
---|
159 | */ |
---|
160 | |
---|
161 | rtems_device_driver Clock_initialize( |
---|
162 | rtems_device_major_number major, |
---|
163 | rtems_device_minor_number minor, |
---|
164 | void *pargp |
---|
165 | ) |
---|
166 | { |
---|
167 | printk("Clock initialize %d %d\n", major, minor); |
---|
168 | |
---|
169 | Install_clock( Clock_isr ); |
---|
170 | |
---|
171 | /* |
---|
172 | * make major/minor avail to others such as shared memory driver |
---|
173 | */ |
---|
174 | |
---|
175 | rtems_clock_major = major; |
---|
176 | rtems_clock_minor = minor; |
---|
177 | |
---|
178 | return RTEMS_SUCCESSFUL; |
---|
179 | } |
---|