[574fb67] | 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 | * |
---|
[0e27119] | 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. |
---|
[574fb67] | 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 | |
---|
[ff8f05b] | 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) |
---|
[574fb67] | 38 | |
---|
| 39 | #define MPC83XX_GTM_CLOCK_MASK MPC83XX_GTM_CLOCK_EXTERN |
---|
| 40 | |
---|
| 41 | static const uint8_t mpc83xx_gmt_interrupt_vector_table [MPC83XX_GTM_NUMBER] = { 90, 78, 84, 72, 91, 79, 85, 73 }; |
---|
| 42 | |
---|
| 43 | static const uint8_t mpc83xx_gmt_interrupt_priority_table [MPC83XX_GTM_NUMBER] = { 113, 77, 95, 59, 116, 80, 98, 62 }; |
---|
| 44 | |
---|
| 45 | rtems_status_code mpc83xx_gtm_initialize( int timer, int clock) |
---|
| 46 | { |
---|
| 47 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
[ff8f05b] | 48 | rtems_interrupt_level level; |
---|
[574fb67] | 49 | |
---|
[ff8f05b] | 50 | unsigned mask = 0xfU << (GTM_LOW(timer) * 4); |
---|
| 51 | unsigned flags = 0x3U << (GTM_LOW(timer) * 4); |
---|
[1dab788] | 52 | uint8_t reg = 0; |
---|
[574fb67] | 53 | |
---|
| 54 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 55 | |
---|
| 56 | rtems_interrupt_disable( level); |
---|
| 57 | |
---|
[ff8f05b] | 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); |
---|
[574fb67] | 61 | |
---|
[ff8f05b] | 62 | mpc83xx.gtm [GTM_MODULE(timer)] |
---|
| 63 | .gt_tim_regs [GTM_HIGH(timer)] |
---|
| 64 | .gtmdr [GTM_LOW(timer)] = 0; |
---|
[574fb67] | 65 | |
---|
| 66 | rtems_interrupt_enable( level); |
---|
| 67 | |
---|
| 68 | sc = mpc83xx_gtm_set_clock( timer, clock); |
---|
[602aee20] | 69 | RTEMS_CHECK_SC( sc, "Set clock"); |
---|
[574fb67] | 70 | |
---|
| 71 | sc = mpc83xx_gtm_set_value( timer, 0); |
---|
[602aee20] | 72 | RTEMS_CHECK_SC( sc, "Set value"); |
---|
[574fb67] | 73 | |
---|
| 74 | sc = mpc83xx_gtm_set_reference( timer, 0); |
---|
[602aee20] | 75 | RTEMS_CHECK_SC( sc, "Set reference"); |
---|
[574fb67] | 76 | |
---|
| 77 | sc = mpc83xx_gtm_set_prescale( timer, 0); |
---|
[602aee20] | 78 | RTEMS_CHECK_SC( sc, "Set prescale"); |
---|
[574fb67] | 79 | |
---|
| 80 | return RTEMS_SUCCESSFUL; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | rtems_status_code mpc83xx_gtm_enable_restart( int timer, bool enable) |
---|
| 84 | { |
---|
[ff8f05b] | 85 | rtems_interrupt_level level; |
---|
[574fb67] | 86 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 87 | |
---|
| 88 | rtems_interrupt_disable( level); |
---|
| 89 | |
---|
| 90 | if (enable) { |
---|
[ff8f05b] | 91 | mpc83xx.gtm [GTM_MODULE(timer)] |
---|
| 92 | .gt_tim_regs [GTM_HIGH(timer)] |
---|
| 93 | .gtmdr [GTM_LOW(timer)] |= 0x0008; |
---|
[574fb67] | 94 | } else { |
---|
[ff8f05b] | 95 | mpc83xx.gtm [GTM_MODULE(timer)] |
---|
| 96 | .gt_tim_regs [GTM_HIGH(timer)] |
---|
| 97 | .gtmdr [GTM_LOW(timer)] &= ~0x0008; |
---|
[574fb67] | 98 | } |
---|
| 99 | |
---|
| 100 | rtems_interrupt_enable( level); |
---|
| 101 | |
---|
| 102 | return RTEMS_SUCCESSFUL; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | rtems_status_code mpc83xx_gtm_set_clock( int timer, int clock) |
---|
| 106 | { |
---|
[ff8f05b] | 107 | rtems_interrupt_level level; |
---|
[574fb67] | 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 | |
---|
[ff8f05b] | 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; |
---|
[574fb67] | 124 | |
---|
| 125 | rtems_interrupt_enable( level); |
---|
| 126 | |
---|
| 127 | return RTEMS_SUCCESSFUL; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | rtems_status_code mpc83xx_gtm_get_clock( int timer, int *clock) |
---|
| 131 | { |
---|
| 132 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 133 | |
---|
[ff8f05b] | 134 | *clock = mpc83xx.gtm [GTM_MODULE(timer)] |
---|
| 135 | .gt_tim_regs [GTM_HIGH(timer)] |
---|
| 136 | .gtmdr [GTM_LOW(timer)] & MPC83XX_GTM_CLOCK_MASK; |
---|
[574fb67] | 137 | |
---|
| 138 | return RTEMS_SUCCESSFUL; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | rtems_status_code mpc83xx_gtm_start( int timer) |
---|
| 142 | { |
---|
[ff8f05b] | 143 | rtems_interrupt_level level; |
---|
| 144 | uint8_t flags = 0x2 << (GTM_LOW(timer) * 4); |
---|
[574fb67] | 145 | |
---|
| 146 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 147 | |
---|
| 148 | rtems_interrupt_disable( level); |
---|
[ff8f05b] | 149 | mpc83xx.gtm [GTM_MODULE(timer)] |
---|
| 150 | .gtcfr [GTM_HIGH(timer)].reg &= ~flags; |
---|
[574fb67] | 151 | rtems_interrupt_enable( level); |
---|
| 152 | |
---|
| 153 | return RTEMS_SUCCESSFUL; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | rtems_status_code mpc83xx_gtm_stop( int timer) |
---|
| 157 | { |
---|
[ff8f05b] | 158 | rtems_interrupt_level level; |
---|
| 159 | uint8_t flags = 0x2 << (GTM_LOW(timer) * 4); |
---|
[574fb67] | 160 | |
---|
| 161 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 162 | |
---|
| 163 | rtems_interrupt_disable( level); |
---|
[ff8f05b] | 164 | mpc83xx.gtm [GTM_MODULE(timer)].gtcfr [GTM_HIGH(timer)].reg |= flags; |
---|
[574fb67] | 165 | rtems_interrupt_enable( level); |
---|
| 166 | |
---|
| 167 | return RTEMS_SUCCESSFUL; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | rtems_status_code mpc83xx_gtm_set_value( int timer, uint16_t value) |
---|
| 171 | { |
---|
| 172 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 173 | |
---|
[ff8f05b] | 174 | mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtcnr [GTM_LOW(timer)] = value; |
---|
[574fb67] | 175 | |
---|
| 176 | return RTEMS_SUCCESSFUL; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | rtems_status_code mpc83xx_gtm_get_value( int timer, uint16_t *value) |
---|
| 180 | { |
---|
| 181 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 182 | |
---|
[ff8f05b] | 183 | *value = mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtcnr [GTM_LOW(timer)]; |
---|
[574fb67] | 184 | |
---|
| 185 | return RTEMS_SUCCESSFUL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | rtems_status_code mpc83xx_gtm_set_reference( int timer, uint16_t reference) |
---|
| 189 | { |
---|
| 190 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 191 | |
---|
[ff8f05b] | 192 | mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtrfr [GTM_LOW(timer)] = reference; |
---|
[574fb67] | 193 | |
---|
| 194 | return RTEMS_SUCCESSFUL; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | rtems_status_code mpc83xx_gtm_get_reference( int timer, uint16_t *reference) |
---|
| 198 | { |
---|
| 199 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 200 | |
---|
[ff8f05b] | 201 | *reference = mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtrfr [GTM_LOW(timer)]; |
---|
[574fb67] | 202 | |
---|
| 203 | return RTEMS_SUCCESSFUL; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | rtems_status_code mpc83xx_gtm_set_prescale( int timer, uint8_t prescale) |
---|
| 207 | { |
---|
| 208 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 209 | |
---|
[ff8f05b] | 210 | mpc83xx.gtm [GTM_MODULE(timer)].gtpsr [GTM_MODULE_TIMER(timer)] = prescale; |
---|
[574fb67] | 211 | |
---|
| 212 | return RTEMS_SUCCESSFUL; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | rtems_status_code mpc83xx_gtm_get_prescale( int timer, uint8_t *prescale) |
---|
| 216 | { |
---|
| 217 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 218 | |
---|
[ff8f05b] | 219 | *prescale = mpc83xx.gtm [GTM_MODULE(timer)].gtpsr [GTM_MODULE_TIMER(timer)]; |
---|
[574fb67] | 220 | |
---|
| 221 | return RTEMS_SUCCESSFUL; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | rtems_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 | |
---|
| 233 | rtems_status_code mpc83xx_gtm_interrupt_enable( int timer) |
---|
| 234 | { |
---|
[ff8f05b] | 235 | rtems_interrupt_level level; |
---|
[574fb67] | 236 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 237 | |
---|
| 238 | rtems_interrupt_disable( level); |
---|
[ff8f05b] | 239 | mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtmdr [GTM_LOW(timer)] |= 0x0010; |
---|
[574fb67] | 240 | rtems_interrupt_enable( level); |
---|
| 241 | |
---|
| 242 | return RTEMS_SUCCESSFUL; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | rtems_status_code mpc83xx_gtm_interrupt_disable( int timer) |
---|
| 246 | { |
---|
[ff8f05b] | 247 | rtems_interrupt_level level; |
---|
[574fb67] | 248 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 249 | |
---|
| 250 | rtems_interrupt_disable( level); |
---|
[ff8f05b] | 251 | mpc83xx.gtm [GTM_MODULE(timer)].gt_tim_regs [GTM_HIGH(timer)].gtmdr [GTM_LOW(timer)] &= ~0x0010; |
---|
[574fb67] | 252 | rtems_interrupt_enable( level); |
---|
| 253 | |
---|
| 254 | return RTEMS_SUCCESSFUL; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | rtems_status_code mpc83xx_gtm_interrupt_clear( int timer) |
---|
| 258 | { |
---|
| 259 | MPC83XX_GTM_CHECK_INDEX( timer); |
---|
| 260 | |
---|
[ff8f05b] | 261 | mpc83xx.gtm [GTM_MODULE(timer)].gtevr [GTM_MODULE_TIMER(timer)] = 0x0002; |
---|
[574fb67] | 262 | |
---|
| 263 | return RTEMS_SUCCESSFUL; |
---|
| 264 | } |
---|