source: rtems/c/src/lib/libcpu/powerpc/mpc83xx/gtm/gtm.c @ 0e27119

4.11
Last change on this file since 0e27119 was 0e27119, checked in by Joel Sherrill <joel.sherrill@…>, on Oct 11, 2012 at 8:52:18 PM

Use proper 3 line form of license text

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/**
2 * @file
3 *
4 * @brief Source file for timer functions.
5 */
6
7/*
8 * Copyright (c) 2008
9 * Embedded Brains GmbH
10 * Obere Lagerstr. 30
11 * D-82178 Puchheim
12 * Germany
13 * rtems@embedded-brains.de
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
20#include <rtems/bspIo.h>
21
22#include <mpc83xx/mpc83xx.h>
23#include <mpc83xx/gtm.h>
24
25#define RTEMS_STATUS_CHECKS_USE_PRINTK
26
27#include <rtems/status-checks.h>
28
29#define MPC83XX_GTM_CHECK_INDEX( timer) \
30        if (( timer) < 0 || ( timer) >= MPC83XX_GTM_NUMBER) { \
31                return RTEMS_INVALID_NUMBER; \
32        }
33
34#define GTM_MODULE(timer)       ((timer)/4)
35#define GTM_MODULE_TIMER(timer) ((timer)%4)
36#define GTM_HIGH(timer)         (GTM_MODULE_TIMER(timer)/2)
37#define GTM_LOW(timer)          (GTM_MODULE_TIMER(timer)%2)
38
39#define MPC83XX_GTM_CLOCK_MASK MPC83XX_GTM_CLOCK_EXTERN
40
41static const uint8_t mpc83xx_gmt_interrupt_vector_table [MPC83XX_GTM_NUMBER] = { 90, 78, 84, 72, 91, 79, 85, 73 };
42
43static const uint8_t mpc83xx_gmt_interrupt_priority_table [MPC83XX_GTM_NUMBER] = { 113, 77, 95, 59, 116, 80, 98, 62 };
44
45rtems_status_code mpc83xx_gtm_initialize( int timer, int clock)
46{
47        rtems_status_code sc = RTEMS_SUCCESSFUL;
48        rtems_interrupt_level level;
49
50        unsigned mask = 0xfU << (GTM_LOW(timer) * 4);
51        unsigned flags = 0x3U << (GTM_LOW(timer) * 4);
52        uint8_t reg = 0;
53
54        MPC83XX_GTM_CHECK_INDEX( timer);
55
56        rtems_interrupt_disable( level);
57
58        reg = mpc83xx.gtm [GTM_MODULE(timer)].gtcfr [GTM_HIGH(timer)].reg;
59        mpc83xx.gtm [GTM_MODULE(timer)].gtcfr [GTM_HIGH(timer)].reg = 
60          (uint8_t) ((reg & ~mask) | flags);
61
62        mpc83xx.gtm [GTM_MODULE(timer)]
63          .gt_tim_regs [GTM_HIGH(timer)]
64          .gtmdr [GTM_LOW(timer)] = 0;
65
66        rtems_interrupt_enable( level);
67
68        sc = mpc83xx_gtm_set_clock( timer, clock);
69        RTEMS_CHECK_SC( sc, "Set clock");
70
71        sc = mpc83xx_gtm_set_value( timer, 0);
72        RTEMS_CHECK_SC( sc, "Set value");
73
74        sc = mpc83xx_gtm_set_reference( timer, 0);
75        RTEMS_CHECK_SC( sc, "Set reference");
76
77        sc = mpc83xx_gtm_set_prescale( timer, 0);
78        RTEMS_CHECK_SC( sc, "Set prescale");
79
80        return RTEMS_SUCCESSFUL;
81}
82
83rtems_status_code mpc83xx_gtm_enable_restart( int timer, bool enable)
84{
85        rtems_interrupt_level level;
86        MPC83XX_GTM_CHECK_INDEX( timer);
87
88        rtems_interrupt_disable( level);
89
90        if (enable) {
91                mpc83xx.gtm [GTM_MODULE(timer)]
92                  .gt_tim_regs [GTM_HIGH(timer)]
93                  .gtmdr [GTM_LOW(timer)] |= 0x0008;
94        } else {
95                mpc83xx.gtm [GTM_MODULE(timer)]
96                  .gt_tim_regs [GTM_HIGH(timer)]
97                  .gtmdr [GTM_LOW(timer)] &= ~0x0008;
98        }
99
100        rtems_interrupt_enable( level);
101
102        return RTEMS_SUCCESSFUL;
103}
104
105rtems_status_code mpc83xx_gtm_set_clock( int timer, int clock)
106{
107        rtems_interrupt_level level;
108        uint16_t reg = 0;
109
110        MPC83XX_GTM_CHECK_INDEX( timer);
111
112        if (clock & ~MPC83XX_GTM_CLOCK_MASK) {
113                return RTEMS_INVALID_CLOCK;
114        }
115
116        rtems_interrupt_disable( level);
117
118        reg = mpc83xx.gtm [GTM_MODULE(timer)]
119          .gt_tim_regs [GTM_HIGH(timer)]
120          .gtmdr [GTM_LOW(timer)];
121        mpc83xx.gtm [GTM_MODULE(timer)]
122          .gt_tim_regs [GTM_HIGH(timer)]
123          .gtmdr [GTM_LOW(timer)] = (reg & ~MPC83XX_GTM_CLOCK_MASK) | clock;
124
125        rtems_interrupt_enable( level);
126
127        return RTEMS_SUCCESSFUL;
128}
129
130rtems_status_code mpc83xx_gtm_get_clock( int timer, int *clock)
131{
132        MPC83XX_GTM_CHECK_INDEX( timer);
133
134        *clock = mpc83xx.gtm [GTM_MODULE(timer)]
135          .gt_tim_regs [GTM_HIGH(timer)]
136          .gtmdr [GTM_LOW(timer)] & MPC83XX_GTM_CLOCK_MASK;
137
138        return RTEMS_SUCCESSFUL;
139}
140
141rtems_status_code mpc83xx_gtm_start( int timer)
142{
143        rtems_interrupt_level level;
144        uint8_t flags = 0x2 << (GTM_LOW(timer) * 4);
145
146        MPC83XX_GTM_CHECK_INDEX( timer);
147
148        rtems_interrupt_disable( level);
149        mpc83xx.gtm [GTM_MODULE(timer)]
150.gtcfr [GTM_HIGH(timer)].reg &= ~flags;
151        rtems_interrupt_enable( level);
152
153        return RTEMS_SUCCESSFUL;
154}
155
156rtems_status_code mpc83xx_gtm_stop( int timer)
157{
158        rtems_interrupt_level level;
159        uint8_t flags = 0x2 << (GTM_LOW(timer) * 4);
160
161        MPC83XX_GTM_CHECK_INDEX( timer);
162
163        rtems_interrupt_disable( level);
164        mpc83xx.gtm [GTM_MODULE(timer)].gtcfr [GTM_HIGH(timer)].reg |= flags;
165        rtems_interrupt_enable( level);
166
167        return RTEMS_SUCCESSFUL;
168}
169
170rtems_status_code mpc83xx_gtm_set_value( int timer, uint16_t value)
171{
172        MPC83XX_GTM_CHECK_INDEX( timer);
173
174        mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtcnr [GTM_LOW(timer)] = value;
175
176        return RTEMS_SUCCESSFUL;
177}
178
179rtems_status_code mpc83xx_gtm_get_value( int timer, uint16_t *value)
180{
181        MPC83XX_GTM_CHECK_INDEX( timer);
182
183        *value = mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtcnr [GTM_LOW(timer)];
184
185        return RTEMS_SUCCESSFUL;
186}
187
188rtems_status_code mpc83xx_gtm_set_reference( int timer, uint16_t reference)
189{
190        MPC83XX_GTM_CHECK_INDEX( timer);
191
192        mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtrfr [GTM_LOW(timer)] = reference;
193
194        return RTEMS_SUCCESSFUL;
195}
196
197rtems_status_code mpc83xx_gtm_get_reference( int timer, uint16_t *reference)
198{
199        MPC83XX_GTM_CHECK_INDEX( timer);
200
201        *reference = mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtrfr [GTM_LOW(timer)];
202
203        return RTEMS_SUCCESSFUL;
204}
205
206rtems_status_code mpc83xx_gtm_set_prescale( int timer, uint8_t prescale)
207{
208        MPC83XX_GTM_CHECK_INDEX( timer);
209
210        mpc83xx.gtm [GTM_MODULE(timer)].gtpsr [GTM_MODULE_TIMER(timer)] = prescale;
211
212        return RTEMS_SUCCESSFUL;
213}
214
215rtems_status_code mpc83xx_gtm_get_prescale( int timer, uint8_t *prescale)
216{
217        MPC83XX_GTM_CHECK_INDEX( timer);
218
219        *prescale = mpc83xx.gtm [GTM_MODULE(timer)].gtpsr [GTM_MODULE_TIMER(timer)];
220
221        return RTEMS_SUCCESSFUL;
222}
223
224rtems_status_code mpc83xx_gtm_interrupt_get_vector( int timer, rtems_vector_number *vector)
225{
226        MPC83XX_GTM_CHECK_INDEX( timer);
227
228        *vector = mpc83xx_gmt_interrupt_vector_table [timer];
229
230        return RTEMS_SUCCESSFUL;
231}
232
233rtems_status_code mpc83xx_gtm_interrupt_enable( int timer)
234{
235        rtems_interrupt_level level;
236        MPC83XX_GTM_CHECK_INDEX( timer);
237
238        rtems_interrupt_disable( level);
239        mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtmdr [GTM_LOW(timer)] |= 0x0010;
240        rtems_interrupt_enable( level);
241
242        return RTEMS_SUCCESSFUL;
243}
244
245rtems_status_code mpc83xx_gtm_interrupt_disable( int timer)
246{
247        rtems_interrupt_level level;
248        MPC83XX_GTM_CHECK_INDEX( timer);
249
250        rtems_interrupt_disable( level);
251        mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtmdr [GTM_LOW(timer)] &= ~0x0010;
252        rtems_interrupt_enable( level);
253
254        return RTEMS_SUCCESSFUL;
255}
256
257rtems_status_code mpc83xx_gtm_interrupt_clear( int timer)
258{
259        MPC83XX_GTM_CHECK_INDEX( timer);
260
261        mpc83xx.gtm [GTM_MODULE(timer)].gtevr [GTM_MODULE_TIMER(timer)] = 0x0002;
262
263        return RTEMS_SUCCESSFUL;
264}
Note: See TracBrowser for help on using the repository browser.