source: rtems/c/src/lib/libbsp/m68k/uC5282/clock/clock.c @ 3f60fb4f

4.115
Last change on this file since 3f60fb4f was 3f60fb4f, checked in by Joel Sherrill <joel.sherrill@…>, on 03/04/11 at 16:03:46

2011-03-04 Till Straumann <strauman@…>

PR 1738/bsps

  • clock/clock.c, include/bsp.h, network/network.c: system clock driver programs the PIT w/o assuming the CPU clock frequency being a power of two.
  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Use the last periodic interval timer (PIT3) as the system clock.
3 *
4 *  Author: W. Eric Norum <norume@aps.anl.gov>
5 *
6 *  COPYRIGHT (c) 2005.
7 *  On-Line Applications Research Corporation (OAR).
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.rtems.com/license/LICENSE.
12 *
13 *  $Id$
14 */
15
16#include <rtems.h>
17#include <bsp.h>
18#include <mcf5282/mcf5282.h>
19
20/*
21 * Use INTC0 base
22 */
23#define CLOCK_VECTOR (64+58)
24
25/*
26 * CPU load counters
27 * Place in static RAM so updates don't hit the SDRAM
28 */
29#define IDLE_COUNTER    __SRAMBASE.idle_counter
30#define FILTERED_IDLE   __SRAMBASE.filtered_idle
31#define MAX_IDLE_COUNT  __SRAMBASE.max_idle_count
32#define PITC_PER_TICK   __SRAMBASE.pitc_per_tick
33#define NSEC_PER_PITC   __SRAMBASE.nsec_per_pitc
34#define FILTER_SHIFT    6
35
36uint32_t bsp_clock_nanoseconds_since_last_tick(void)
37{
38    int i = MCF5282_PIT3_PCNTR;
39    if (MCF5282_PIT3_PCSR & MCF5282_PIT_PCSR_PIF)
40        i = MCF5282_PIT3_PCNTR - PITC_PER_TICK;
41    return (PITC_PER_TICK - i) * NSEC_PER_PITC;
42}
43
44#define Clock_driver_nanoseconds_since_last_tick bsp_clock_nanoseconds_since_last_tick
45
46/*
47 * Periodic interval timer interrupt handler
48 */
49#define Clock_driver_support_at_tick()                                       \
50    do {                                                                     \
51        unsigned idle = IDLE_COUNTER;                                        \
52        IDLE_COUNTER = 0;                                                    \
53        if (idle > MAX_IDLE_COUNT)                                           \
54            MAX_IDLE_COUNT = idle;                                           \
55        FILTERED_IDLE = idle + FILTERED_IDLE - (FILTERED_IDLE>>FILTER_SHIFT);\
56        MCF5282_PIT3_PCSR |= MCF5282_PIT_PCSR_PIF;                           \
57    } while (0)
58
59/*
60 * Attach clock interrupt handler
61 */
62#define Clock_driver_support_install_isr( _new, _old )              \
63    do {                                                            \
64        _old = (rtems_isr_entry)set_vector(_new, CLOCK_VECTOR, 1);  \
65    } while(0)
66
67/*
68 * Turn off the clock
69 */
70#define Clock_driver_support_shutdown_hardware()   \
71    do {                                           \
72        MCF5282_PIT3_PCSR &= ~MCF5282_PIT_PCSR_EN; \
73    } while(0)
74
75/*
76 * Set up the clock hardware
77 *
78 * f_pit = f_clk / 2^(preScaleCode+1) / N  = 1/(us_per_tick/us_per_s)
79 *
80 * N = f_clk / 2^(preScaleCode+1) * us_per_tick / us_per_s
81 *
82 * ns_per_pit_clk = ns_per_s / (f_clk / 2^(preScaleCode+1))
83 *                = ns_per_s * 2^(preScaleCode+1) / f_clk;
84 */
85#define Clock_driver_support_initialize_hardware()                       \
86    do {                                                                 \
87                unsigned long long N;                                            \
88        int level;                                                       \
89        int preScaleCode = 0;                                            \
90                N  = bsp_get_CPU_clock_speed();                                  \
91                N *= rtems_configuration_get_microseconds_per_tick();            \
92                N /= 2*1000000; /* min_prescale * us_per_s */                    \
93                while ( N > 0x10000 ) {                                          \
94                        preScaleCode++;                                              \
95                        N >>= 1;                                                     \
96                }                                                                \
97                PITC_PER_TICK  = N;                                              \
98                N  = 2000000000ULL << preScaleCode;                              \
99                N /= bsp_get_CPU_clock_speed();                                  \
100                NSEC_PER_PITC  = N;                                              \
101        IDLE_COUNTER   = 0;                                              \
102        FILTERED_IDLE  = 0;                                              \
103        MAX_IDLE_COUNT = 0;                                              \
104        bsp_allocate_interrupt(PIT3_IRQ_LEVEL, PIT3_IRQ_PRIORITY);       \
105        MCF5282_INTC0_ICR58 = MCF5282_INTC_ICR_IL(PIT3_IRQ_LEVEL) |      \
106                              MCF5282_INTC_ICR_IP(PIT3_IRQ_PRIORITY);    \
107        rtems_interrupt_disable( level );                                \
108        MCF5282_INTC0_IMRH &= ~MCF5282_INTC_IMRH_INT58;                  \
109        MCF5282_PIT3_PCSR &= ~MCF5282_PIT_PCSR_EN;                       \
110        rtems_interrupt_enable( level );                                 \
111        MCF5282_PIT3_PCSR = MCF5282_PIT_PCSR_PRE(preScaleCode) |         \
112                            MCF5282_PIT_PCSR_OVW |                       \
113                            MCF5282_PIT_PCSR_PIE |                       \
114                            MCF5282_PIT_PCSR_RLD;                        \
115        MCF5282_PIT3_PMR = PITC_PER_TICK - 1;                            \
116        MCF5282_PIT3_PCSR = MCF5282_PIT_PCSR_PRE(preScaleCode) |         \
117                            MCF5282_PIT_PCSR_PIE |                       \
118                            MCF5282_PIT_PCSR_RLD |                       \
119                            MCF5282_PIT_PCSR_EN;                         \
120    } while (0)
121
122/*
123 * Provide our own version of the idle task
124 */
125Thread bsp_idle_thread(uint32_t ignored)
126{
127  /* Atomic increment */
128  for(;;)
129    __asm__ volatile ("addq.l #1,%0"::"m"(IDLE_COUNTER));
130}
131
132int rtems_bsp_cpu_load_percentage(void)
133{
134    return MAX_IDLE_COUNT ?
135           (100 - ((100 * (FILTERED_IDLE >> FILTER_SHIFT)) / MAX_IDLE_COUNT)) :
136           0;
137}
138
139#include "../../../shared/clockdrv_shell.h"
Note: See TracBrowser for help on using the repository browser.