[d374492] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup mpc55xx |
---|
| 5 | * |
---|
| 6 | * @brief Clock driver configuration. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | * Copyright (c) 2009 |
---|
| 11 | * embedded brains GmbH |
---|
| 12 | * Obere Lagerstr. 30 |
---|
| 13 | * D-82178 Puchheim |
---|
| 14 | * Germany |
---|
| 15 | * <rtems@embedded-brains.de> |
---|
| 16 | * |
---|
| 17 | * The license and distribution terms for this file may be |
---|
| 18 | * found in the file LICENSE in this distribution or at |
---|
| 19 | * http://www.rtems.com/license/LICENSE. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <mpc55xx/regs.h> |
---|
| 23 | #include <mpc55xx/emios.h> |
---|
| 24 | |
---|
| 25 | #include <rtems.h> |
---|
| 26 | |
---|
| 27 | #include <bsp.h> |
---|
| 28 | #include <bsp/irq.h> |
---|
| 29 | |
---|
| 30 | #define RTEMS_STATUS_CHECKS_USE_PRINTK |
---|
| 31 | |
---|
| 32 | #include <rtems/status-checks.h> |
---|
| 33 | |
---|
| 34 | #define MPC55XX_CLOCK_EMIOS_CHANNEL (MPC55XX_EMIOS_CHANNEL_NUMBER - 1) |
---|
| 35 | |
---|
| 36 | /* This is defined in clockdrv_shell.h */ |
---|
| 37 | rtems_isr Clock_isr( rtems_vector_number vector); |
---|
| 38 | |
---|
| 39 | #define Clock_driver_support_at_tick() \ |
---|
| 40 | do { \ |
---|
| 41 | union EMIOS_CSR_tag csr = MPC55XX_ZERO_FLAGS; \ |
---|
| 42 | csr.B.FLAG = 1; \ |
---|
| 43 | EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL].CSR.R = csr.R; \ |
---|
| 44 | } while (0) |
---|
| 45 | |
---|
| 46 | static void mpc55xx_clock_handler_install( void) |
---|
| 47 | { |
---|
| 48 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 49 | |
---|
| 50 | sc = mpc55xx_interrupt_handler_install( |
---|
| 51 | MPC55XX_IRQ_EMIOS_GET_REQUEST( MPC55XX_CLOCK_EMIOS_CHANNEL), |
---|
| 52 | "clock", |
---|
| 53 | RTEMS_INTERRUPT_UNIQUE, |
---|
| 54 | MPC55XX_INTC_MIN_PRIORITY, |
---|
| 55 | (rtems_interrupt_handler) Clock_isr, |
---|
| 56 | NULL |
---|
| 57 | ); |
---|
| 58 | RTEMS_CHECK_SC_VOID( sc, "install clock interrupt handler"); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | static void mpc55xx_clock_initialize( void) |
---|
| 62 | { |
---|
| 63 | volatile struct EMIOS_CH_tag *regs = &EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL]; |
---|
| 64 | union EMIOS_CCR_tag ccr = MPC55XX_ZERO_FLAGS; |
---|
| 65 | union EMIOS_CSR_tag csr = MPC55XX_ZERO_FLAGS; |
---|
| 66 | unsigned prescaler = mpc55xx_emios_global_prescaler(); |
---|
| 67 | uint64_t interval = ((uint64_t) bsp_clock_speed |
---|
| 68 | * (uint64_t) rtems_configuration_get_microseconds_per_tick()) / 1000000; |
---|
| 69 | |
---|
| 70 | /* Apply prescaler */ |
---|
| 71 | if (prescaler > 0) { |
---|
| 72 | interval /= (uint64_t) prescaler; |
---|
| 73 | } else { |
---|
| 74 | RTEMS_SYSLOG_ERROR( "unexpected global eMIOS prescaler\n"); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /* Check interval */ |
---|
| 78 | if (interval == 0 || interval > MPC55XX_EMIOS_VALUE_MAX) { |
---|
| 79 | interval = MPC55XX_EMIOS_VALUE_MAX; |
---|
| 80 | RTEMS_SYSLOG_ERROR( "clock timer interval out of range\n"); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /* Configure eMIOS channel */ |
---|
[ac7af4a] | 84 | |
---|
[d374492] | 85 | /* Set channel in GPIO mode */ |
---|
| 86 | ccr.B.MODE = MPC55XX_EMIOS_MODE_GPIO_INPUT; |
---|
| 87 | regs->CCR.R = ccr.R; |
---|
[ac7af4a] | 88 | |
---|
[d374492] | 89 | /* Clear status flags */ |
---|
| 90 | csr.B.OVR = 1; |
---|
| 91 | csr.B.OVFL = 1; |
---|
| 92 | csr.B.FLAG = 1; |
---|
| 93 | regs->CSR.R = csr.R; |
---|
[ac7af4a] | 94 | |
---|
[d374492] | 95 | /* Set timer period */ |
---|
| 96 | regs->CADR.R = (uint32_t) interval - 1; |
---|
[ac7af4a] | 97 | |
---|
[d374492] | 98 | /* Set unused registers */ |
---|
| 99 | regs->CBDR.R = 0; |
---|
| 100 | regs->CCNTR.R = 0; |
---|
| 101 | regs->ALTCADR.R = 0; |
---|
[ac7af4a] | 102 | |
---|
[d374492] | 103 | /* Set control register */ |
---|
| 104 | ccr.B.MODE = MPC55XX_EMIOS_MODE_MC_UP_INT_CLK; |
---|
| 105 | ccr.B.UCPREN = 1; |
---|
| 106 | ccr.B.FEN = 1; |
---|
| 107 | ccr.B.FREN = 1; |
---|
| 108 | regs->CCR.R = ccr.R; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | static void mpc55xx_clock_cleanup( void) |
---|
| 112 | { |
---|
| 113 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 114 | volatile struct EMIOS_CH_tag *regs = &EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL]; |
---|
| 115 | union EMIOS_CCR_tag ccr = MPC55XX_ZERO_FLAGS; |
---|
| 116 | |
---|
| 117 | /* Set channel in GPIO mode */ |
---|
| 118 | ccr.B.MODE = MPC55XX_EMIOS_MODE_GPIO_INPUT; |
---|
| 119 | regs->CCR.R = ccr.R; |
---|
| 120 | |
---|
| 121 | /* Remove interrupt handler */ |
---|
| 122 | sc = rtems_interrupt_handler_remove( |
---|
| 123 | MPC55XX_IRQ_EMIOS_GET_REQUEST( MPC55XX_CLOCK_EMIOS_CHANNEL), |
---|
| 124 | (rtems_interrupt_handler) Clock_isr, |
---|
| 125 | NULL |
---|
| 126 | ); |
---|
| 127 | RTEMS_CHECK_SC_VOID( sc, "remove clock interrupt handler"); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | static uint32_t mpc55xx_clock_nanoseconds_since_last_tick( void) |
---|
| 131 | { |
---|
| 132 | uint64_t clicks = EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL].CCNTR.R; |
---|
| 133 | uint64_t clock = bsp_clock_speed; |
---|
| 134 | uint64_t ns = (clicks * 1000000000) / clock; |
---|
[ac7af4a] | 135 | |
---|
[d374492] | 136 | return (uint32_t) ns; |
---|
| 137 | } |
---|
[ac7af4a] | 138 | |
---|
[d374492] | 139 | #define Clock_driver_support_initialize_hardware() mpc55xx_clock_initialize() |
---|
| 140 | |
---|
| 141 | #define Clock_driver_support_install_isr( isr, old_isr) \ |
---|
| 142 | mpc55xx_clock_handler_install() |
---|
| 143 | |
---|
| 144 | #define Clock_driver_support_shutdown_hardware() mpc55xx_clock_cleanup() |
---|
[ac7af4a] | 145 | |
---|
[d374492] | 146 | #define Clock_driver_nanoseconds_since_last_tick \ |
---|
| 147 | mpc55xx_clock_nanoseconds_since_last_tick |
---|
| 148 | |
---|
| 149 | /* Include shared source clock driver code */ |
---|
| 150 | #include "../../../../libbsp/shared/clockdrv_shell.h" |
---|