summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/mpc55xxevb/clock/clock-config.c
blob: d8a6594ad215d6823d6b9aa5780a30125a95286b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * @file
 *
 * @ingroup mpc55xx
 *
 * @brief Clock driver configuration.
 */

/*
 * Copyright (c) 2009-2011 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#include <bsp.h>
#include <bsp/irq.h>

#include <mpc55xx/regs.h>

static uint64_t mpc55xx_clock_factor;

#if defined(MPC55XX_CLOCK_EMIOS_CHANNEL)

#include <mpc55xx/emios.h>

static void mpc55xx_clock_at_tick(void)
{
  union EMIOS_CSR_tag csr = MPC55XX_ZERO_FLAGS;
  csr.B.FLAG = 1;
  EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL].CSR.R = csr.R;
}

static void mpc55xx_clock_handler_install(rtems_isr_entry isr)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = mpc55xx_interrupt_handler_install(
    MPC55XX_IRQ_EMIOS(MPC55XX_CLOCK_EMIOS_CHANNEL),
    "clock",
    RTEMS_INTERRUPT_UNIQUE,
    MPC55XX_INTC_MIN_PRIORITY,
    (rtems_interrupt_handler) isr,
    NULL
  );
  if (sc != RTEMS_SUCCESSFUL) {
    rtems_fatal_error_occurred(0xdeadbeef);
  }
}

static void mpc55xx_clock_initialize(void)
{
  volatile struct EMIOS_CH_tag *regs = &EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL];
  union EMIOS_CCR_tag ccr = MPC55XX_ZERO_FLAGS;
  union EMIOS_CSR_tag csr = MPC55XX_ZERO_FLAGS;
  unsigned prescaler = mpc55xx_emios_global_prescaler();
  uint64_t reference_clock = bsp_clock_speed;
  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
  uint64_t interval = (reference_clock * us_per_tick) / 1000000;

  mpc55xx_clock_factor = (1000000000ULL << 32) / reference_clock;

  /* Apply prescaler */
  if (prescaler > 0) {
    interval /= (uint64_t) prescaler;
  } else {
    rtems_fatal_error_occurred(0xdeadbeef);
  }

  /* Check interval */
  if (interval == 0 || interval > MPC55XX_EMIOS_VALUE_MAX) {
    rtems_fatal_error_occurred(0xdeadbeef);
  }

  /* Configure eMIOS channel */

  /* Set channel in GPIO mode */
  ccr.B.MODE = MPC55XX_EMIOS_MODE_GPIO_INPUT;
  regs->CCR.R = ccr.R;

  /* Clear status flags */
  csr.B.OVR = 1;
  csr.B.OVFL = 1;
  csr.B.FLAG = 1;
  regs->CSR.R = csr.R;

  /* Set internal counter start value */
  regs->CCNTR.R = 1;

  /* Set timer period */
  regs->CADR.R = (uint32_t) interval - 1;

  /* Set control register */
  #if MPC55XX_CHIP_TYPE / 10 == 551
    ccr.B.MODE = MPC55XX_EMIOS_MODE_MCB_UP_INT_CLK;
  #else
    ccr.B.MODE = MPC55XX_EMIOS_MODE_MC_UP_INT_CLK;
  #endif
  ccr.B.UCPREN = 1;
  ccr.B.FEN = 1;
  ccr.B.FREN = 1;
  regs->CCR.R = ccr.R;
}

static void mpc55xx_clock_cleanup(void)
{
  volatile struct EMIOS_CH_tag *regs = &EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL];
  union EMIOS_CCR_tag ccr = MPC55XX_ZERO_FLAGS;

  /* Set channel in GPIO mode */
  ccr.B.MODE = MPC55XX_EMIOS_MODE_GPIO_INPUT;
  regs->CCR.R = ccr.R;
}

static uint32_t mpc55xx_clock_nanoseconds_since_last_tick(void)
{
  volatile struct EMIOS_CH_tag *regs = &EMIOS.CH [MPC55XX_CLOCK_EMIOS_CHANNEL];
  uint64_t c = regs->CCNTR.R;
  union EMIOS_CSR_tag csr = { .R = regs->CSR.R };
  uint64_t k = mpc55xx_clock_factor;

  if (csr.B.FLAG != 0) {
    c = regs->CCNTR.R + regs->CADR.R + 1;
  }

  return (uint32_t) ((c * k) >> 32);
}

#elif defined(MPC55XX_CLOCK_PIT_CHANNEL)

static void mpc55xx_clock_at_tick(void)
{
  volatile PIT_RTI_CHANNEL_tag *channel =
    &PIT_RTI.CHANNEL [MPC55XX_CLOCK_PIT_CHANNEL];
  PIT_RTI_TFLG_32B_tag tflg = { .B = { .TIF = 1 } };

  channel->TFLG.R = tflg.R;
}

static void mpc55xx_clock_handler_install(rtems_isr_entry isr)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = mpc55xx_interrupt_handler_install(
    MPC55XX_IRQ_PIT_CHANNEL(MPC55XX_CLOCK_PIT_CHANNEL),
    "clock",
    RTEMS_INTERRUPT_UNIQUE,
    MPC55XX_INTC_MIN_PRIORITY,
    (rtems_interrupt_handler) isr,
    NULL
  );
  if (sc != RTEMS_SUCCESSFUL) {
    rtems_fatal_error_occurred(0xdeadbeef);
  }
}

static void mpc55xx_clock_initialize(void)
{
  volatile PIT_RTI_CHANNEL_tag *channel =
    &PIT_RTI.CHANNEL [MPC55XX_CLOCK_PIT_CHANNEL];
  uint64_t reference_clock = bsp_clock_speed;
  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
  uint64_t interval = (reference_clock * us_per_tick) / 1000000;
  PIT_RTI_PITMCR_32B_tag pitmcr = { .B = { .FRZ = 1 } };
  PIT_RTI_TCTRL_32B_tag tctrl = { .B = { .TIE = 1, .TEN = 1 } };

  mpc55xx_clock_factor = (1000000000ULL << 32) / reference_clock;

  PIT_RTI.PITMCR.R = pitmcr.R;
  channel->LDVAL.R = interval;
  channel->TCTRL.R = tctrl.R;
}

static void mpc55xx_clock_cleanup(void)
{
  volatile PIT_RTI_CHANNEL_tag *channel =
    &PIT_RTI.CHANNEL [MPC55XX_CLOCK_PIT_CHANNEL];

  channel->TCTRL.R = 0;
}

static uint32_t mpc55xx_clock_nanoseconds_since_last_tick(void)
{
  volatile PIT_RTI_CHANNEL_tag *channel =
    &PIT_RTI.CHANNEL [MPC55XX_CLOCK_PIT_CHANNEL];
  uint32_t c = channel->CVAL.R;
  uint32_t i = channel->LDVAL.R;
  uint64_t k = mpc55xx_clock_factor;

  if (channel->TFLG.B.TIF != 0) {
    c = channel->CVAL.R - i;
  }

  return (uint32_t) (((i - c) * k) >> 32);
}

#endif

#define Clock_driver_support_at_tick() \
  mpc55xx_clock_at_tick()
#define Clock_driver_support_initialize_hardware() \
  mpc55xx_clock_initialize()
#define Clock_driver_support_install_isr(isr, old_isr) \
  do { \
    mpc55xx_clock_handler_install(isr); \
    old_isr = NULL; \
  } while (0)
#define Clock_driver_support_shutdown_hardware() \
  mpc55xx_clock_cleanup()
#define Clock_driver_nanoseconds_since_last_tick \
  mpc55xx_clock_nanoseconds_since_last_tick

/* Include shared source clock driver code */
#include "../../../../libbsp/shared/clockdrv_shell.h"