source: rtems/bsps/powerpc/shared/clock/clock.c @ bb99cd0d

5
Last change on this file since bb99cd0d was bb99cd0d, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 5, 2019 at 6:22:33 PM

clock: Simplify driver initialization

Use a system initialization handler instead of a legacy IO driver.

Update #3834.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup RTEMSBSPsPowerPCShared
5 *
6 * @brief Source file for a clock driver.
7 */
8
9/*
10 * Copyright (c) 2008-2015 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#include <rtems.h>
24#include <rtems/clockdrv.h>
25#include <rtems/timecounter.h>
26
27#include <libcpu/powerpc-utility.h>
28#include <bsp/vectors.h>
29
30#define RTEMS_STATUS_CHECKS_USE_PRINTK
31
32#include <rtems/status-checks.h>
33
34/*
35 * This variable must be defined in the BSP and valid before clock driver
36 * initialization.
37 */
38extern uint32_t bsp_time_base_frequency;
39
40#define PPC405_PIT 0x3db
41
42#define PPC_CLOCK_DECREMENTER_MAX UINT32_MAX
43
44volatile uint32_t Clock_driver_ticks = 0;
45
46static uint32_t ppc_clock_decrementer_value = PPC_CLOCK_DECREMENTER_MAX;
47
48static uint32_t ppc_clock_next_time_base;
49
50static struct timecounter ppc_tc;
51
52static uint32_t ppc_get_timecount(struct timecounter *tc)
53{
54  return ppc_time_base();
55}
56
57static void ppc_clock_no_tick(void)
58{
59  /* Do nothing */
60}
61
62static void (*ppc_clock_tick)(void) = ppc_clock_no_tick;
63
64static int ppc_clock_exception_handler(
65  BSP_Exception_frame *frame,
66  unsigned number
67)
68{
69  uint32_t delta = ppc_clock_decrementer_value;
70  uint32_t next = ppc_clock_next_time_base;
71  uint32_t dec = 0;
72  uint32_t now = 0;
73  uint32_t msr = 0;
74
75  do {
76    /* Increment clock ticks */
77    Clock_driver_ticks += 1;
78
79    /* Enable external exceptions */
80    msr = ppc_external_exceptions_enable();
81
82    /* Call clock ticker  */
83    ppc_clock_tick();
84
85    /* Restore machine state */
86    ppc_external_exceptions_disable( msr);
87
88    /* Next time base */
89    next += delta;
90
91    /* Current time */
92    now = ppc_time_base();
93
94    /* New decrementer value */
95    dec = next - now;
96  } while (dec > delta);
97
98  /* Set decrementer */
99  ppc_set_decrementer_register( dec);
100
101  /* Expected next time base */
102  ppc_clock_next_time_base = next;
103
104  return 0;
105}
106
107static int ppc_clock_exception_handler_first(
108  BSP_Exception_frame *frame,
109  unsigned number
110)
111{
112  /* We have to clear the first pending decrementer exception this way */
113
114  if (ppc_decrementer_register() >= 0x80000000) {
115    ppc_clock_exception_handler( frame, number);
116  }
117
118  ppc_exc_set_handler( ASM_DEC_VECTOR, ppc_clock_exception_handler);
119
120  return 0;
121}
122
123static int ppc_clock_exception_handler_booke(
124  BSP_Exception_frame *frame,
125  unsigned number
126)
127{
128  uint32_t msr;
129
130  /* Acknowledge decrementer request */
131  PPC_SET_SPECIAL_PURPOSE_REGISTER( BOOKE_TSR, BOOKE_TSR_DIS);
132
133  /* Increment clock ticks */
134  Clock_driver_ticks += 1;
135
136  /* Enable external exceptions */
137  msr = ppc_external_exceptions_enable();
138
139  /* Call clock ticker  */
140  ppc_clock_tick();
141
142  /* Restore machine state */
143  ppc_external_exceptions_disable( msr);
144
145  return 0;
146}
147
148static int ppc_clock_exception_handler_ppc405(BSP_Exception_frame *frame, unsigned number)
149{
150  uint32_t msr;
151
152  /* Acknowledge PIT request */
153  PPC_SET_SPECIAL_PURPOSE_REGISTER(PPC405_TSR, BOOKE_TSR_DIS);
154
155  /* Increment clock ticks */
156  Clock_driver_ticks += 1;
157
158  /* Enable external exceptions */
159  msr = ppc_external_exceptions_enable();
160
161  /* Call clock ticker  */
162  ppc_clock_tick();
163
164  /* Restore machine state */
165  ppc_external_exceptions_disable(msr);
166
167  return 0;
168}
169
170void _Clock_Initialize( void )
171{
172  uint64_t frequency = bsp_time_base_frequency;
173  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
174  uint32_t interval = (uint32_t) ((frequency * us_per_tick) / 1000000);
175
176  /*
177   * Set default ticker.
178   */
179  ppc_clock_tick = rtems_timecounter_tick;
180
181  if (ppc_cpu_is_bookE() != PPC_BOOKE_405) {
182    /* Decrementer value */
183    ppc_clock_decrementer_value = interval - 1;
184
185    /* Check decrementer value */
186    if (ppc_clock_decrementer_value == 0) {
187      ppc_clock_decrementer_value = PPC_CLOCK_DECREMENTER_MAX;
188      RTEMS_SYSLOG_ERROR( "decrementer value would be zero, will be set to maximum value instead\n");
189    }
190    if (ppc_cpu_is_bookE()) {
191      /* Set decrementer auto-reload value */
192      PPC_SET_SPECIAL_PURPOSE_REGISTER( BOOKE_DECAR, ppc_clock_decrementer_value);
193
194      /* Install exception handler */
195      ppc_exc_set_handler( ASM_BOOKE_DEC_VECTOR, ppc_clock_exception_handler_booke);
196
197      /* Enable decrementer and auto-reload */
198      PPC_SET_SPECIAL_PURPOSE_REGISTER_BITS( BOOKE_TCR, BOOKE_TCR_DIE | BOOKE_TCR_ARE);
199    } else {
200      /* Here the decrementer value is actually the interval */
201      ++ppc_clock_decrementer_value;
202
203      /* Initialize next time base */
204      ppc_clock_next_time_base = ppc_time_base() + ppc_clock_decrementer_value;
205
206      /* Install exception handler */
207      ppc_exc_set_handler( ASM_DEC_VECTOR, ppc_clock_exception_handler_first);
208    }
209
210    /* Set the decrementer value */
211    ppc_set_decrementer_register( ppc_clock_decrementer_value);
212  } else {
213    /* PIT interval value */
214    ppc_clock_decrementer_value = interval;
215
216    /* Install exception handler */
217    ppc_exc_set_handler(ASM_BOOKE_DEC_VECTOR, ppc_clock_exception_handler_ppc405);
218
219    /* Enable PIT and auto-reload */
220    PPC_SET_SPECIAL_PURPOSE_REGISTER_BITS(PPC405_TCR, BOOKE_TCR_DIE | BOOKE_TCR_ARE);
221
222    /* Set PIT auto-reload and initial value */
223    PPC_SET_SPECIAL_PURPOSE_REGISTER(PPC405_PIT, interval);
224  }
225
226  /* Install timecounter */
227  ppc_tc.tc_get_timecount = ppc_get_timecount;
228  ppc_tc.tc_counter_mask = 0xffffffff;
229  ppc_tc.tc_frequency = frequency;
230  ppc_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
231  rtems_timecounter_install(&ppc_tc);
232}
Note: See TracBrowser for help on using the repository browser.