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

5
Last change on this file since a6b2080 was a6b2080, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 5, 2019 at 2:51:17 PM

clock: Remove Clock_exit() from API

This function is no longer supported by the standard clock driver
implementation (clockimpl.h).

Update #3436.

  • Property mode set to 100644
File size: 5.7 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
170rtems_device_driver Clock_initialize(
171  rtems_device_major_number major,
172  rtems_device_minor_number minor,
173  void *arg
174)
175{
176  uint64_t frequency = bsp_time_base_frequency;
177  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
178  uint32_t interval = (uint32_t) ((frequency * us_per_tick) / 1000000);
179
180  /*
181   * Set default ticker.
182   */
183  ppc_clock_tick = rtems_timecounter_tick;
184
185  if (ppc_cpu_is_bookE() != PPC_BOOKE_405) {
186    /* Decrementer value */
187    ppc_clock_decrementer_value = interval - 1;
188
189    /* Check decrementer value */
190    if (ppc_clock_decrementer_value == 0) {
191      ppc_clock_decrementer_value = PPC_CLOCK_DECREMENTER_MAX;
192      RTEMS_SYSLOG_ERROR( "decrementer value would be zero, will be set to maximum value instead\n");
193    }
194    if (ppc_cpu_is_bookE()) {
195      /* Set decrementer auto-reload value */
196      PPC_SET_SPECIAL_PURPOSE_REGISTER( BOOKE_DECAR, ppc_clock_decrementer_value);
197
198      /* Install exception handler */
199      ppc_exc_set_handler( ASM_BOOKE_DEC_VECTOR, ppc_clock_exception_handler_booke);
200
201      /* Enable decrementer and auto-reload */
202      PPC_SET_SPECIAL_PURPOSE_REGISTER_BITS( BOOKE_TCR, BOOKE_TCR_DIE | BOOKE_TCR_ARE);
203    } else {
204      /* Here the decrementer value is actually the interval */
205      ++ppc_clock_decrementer_value;
206
207      /* Initialize next time base */
208      ppc_clock_next_time_base = ppc_time_base() + ppc_clock_decrementer_value;
209
210      /* Install exception handler */
211      ppc_exc_set_handler( ASM_DEC_VECTOR, ppc_clock_exception_handler_first);
212    }
213
214    /* Set the decrementer value */
215    ppc_set_decrementer_register( ppc_clock_decrementer_value);
216  } else {
217    /* PIT interval value */
218    ppc_clock_decrementer_value = interval;
219
220    /* Install exception handler */
221    ppc_exc_set_handler(ASM_BOOKE_DEC_VECTOR, ppc_clock_exception_handler_ppc405);
222
223    /* Enable PIT and auto-reload */
224    PPC_SET_SPECIAL_PURPOSE_REGISTER_BITS(PPC405_TCR, BOOKE_TCR_DIE | BOOKE_TCR_ARE);
225
226    /* Set PIT auto-reload and initial value */
227    PPC_SET_SPECIAL_PURPOSE_REGISTER(PPC405_PIT, interval);
228  }
229
230  /* Install timecounter */
231  ppc_tc.tc_get_timecount = ppc_get_timecount;
232  ppc_tc.tc_counter_mask = 0xffffffff;
233  ppc_tc.tc_frequency = frequency;
234  ppc_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
235  rtems_timecounter_install(&ppc_tc);
236
237  return RTEMS_SUCCESSFUL;
238}
Note: See TracBrowser for help on using the repository browser.