Ticket #1738: rtems-PR#1738-uC5282-clock.diff

File rtems-PR#1738-uC5282-clock.diff, 10.2 KB (added by strauman, on 02/22/11 at 17:16:02)

additional patch fixing system clock rate for non-64MHz boards

Line 
1Index: c/src/lib/libbsp/m68k/uC5282/clock/clock.c
2===================================================================
3RCS file: /usr1/CVS/rtems/c/src/lib/libbsp/m68k/uC5282/clock/clock.c,v
4retrieving revision 1.16
5diff -c -r1.16 clock.c
6*** c/src/lib/libbsp/m68k/uC5282/clock/clock.c  26 Aug 2009 13:32:22 -0000      1.16
7--- c/src/lib/libbsp/m68k/uC5282/clock/clock.c  22 Feb 2011 18:09:34 -0000
8***************
9*** 26,44 ****
10   * CPU load counters
11   * Place in static RAM so updates don't hit the SDRAM
12   */
13! extern int __SRAMBASE[];
14! #define IDLE_COUNTER      __SRAMBASE[0]
15! #define FILTERED_IDLE     __SRAMBASE[1]
16! #define MAX_IDLE_COUNT    __SRAMBASE[2]
17! #define USEC_PER_TICK     __SRAMBASE[3]
18  #define FILTER_SHIFT    6
19 
20  uint32_t bsp_clock_nanoseconds_since_last_tick(void)
21  {
22      int i = MCF5282_PIT3_PCNTR;
23      if (MCF5282_PIT3_PCSR & MCF5282_PIT_PCSR_PIF)
24!         i = MCF5282_PIT3_PCNTR - USEC_PER_TICK;
25!     return (USEC_PER_TICK - i) * 1000;
26  }
27 
28  #define Clock_driver_nanoseconds_since_last_tick bsp_clock_nanoseconds_since_last_tick
29--- 26,44 ----
30   * CPU load counters
31   * Place in static RAM so updates don't hit the SDRAM
32   */
33! #define IDLE_COUNTER    __SRAMBASE.idle_counter
34! #define FILTERED_IDLE   __SRAMBASE.filtered_idle
35! #define MAX_IDLE_COUNT  __SRAMBASE.max_idle_count
36! #define PITC_PER_TICK   __SRAMBASE.pitc_per_tick
37! #define NSEC_PER_PITC   __SRAMBASE.nsec_per_pitc
38  #define FILTER_SHIFT    6
39 
40  uint32_t bsp_clock_nanoseconds_since_last_tick(void)
41  {
42      int i = MCF5282_PIT3_PCNTR;
43      if (MCF5282_PIT3_PCSR & MCF5282_PIT_PCSR_PIF)
44!         i = MCF5282_PIT3_PCNTR - PITC_PER_TICK;
45!     return (PITC_PER_TICK - i) * NSEC_PER_PITC;
46  }
47 
48  #define Clock_driver_nanoseconds_since_last_tick bsp_clock_nanoseconds_since_last_tick
49***************
50*** 48,54 ****
51   */
52  #define Clock_driver_support_at_tick()                                       \
53      do {                                                                     \
54!         int idle = IDLE_COUNTER;                                             \
55          IDLE_COUNTER = 0;                                                    \
56          if (idle > MAX_IDLE_COUNT)                                           \
57              MAX_IDLE_COUNT = idle;                                           \
58--- 48,54 ----
59   */
60  #define Clock_driver_support_at_tick()                                       \
61      do {                                                                     \
62!         unsigned idle = IDLE_COUNTER;                                        \
63          IDLE_COUNTER = 0;                                                    \
64          if (idle > MAX_IDLE_COUNT)                                           \
65              MAX_IDLE_COUNT = idle;                                           \
66***************
67*** 75,94 ****
68  /*
69   * Set up the clock hardware
70   *
71!  * Prescale so that it counts in microseconds
72!  * System clock frequency better be 2**n (1<=n<=16) MHz!
73   */
74  #define Clock_driver_support_initialize_hardware()                       \
75      do {                                                                 \
76          int level;                                                       \
77!         int preScaleCode = -2;                                           \
78!         int preScaleDivisor = bsp_get_CPU_clock_speed() / 1000000;       \
79!         while (preScaleDivisor) {                                        \
80!             preScaleDivisor >>= 1;                                       \
81!             preScaleCode++;                                              \
82!         }                                                                \
83!         IDLE_COUNTER = 0;                                                \
84!         FILTERED_IDLE = 0;                                               \
85          MAX_IDLE_COUNT = 0;                                              \
86          bsp_allocate_interrupt(PIT3_IRQ_LEVEL, PIT3_IRQ_PRIORITY);       \
87          MCF5282_INTC0_ICR58 = MCF5282_INTC_ICR_IL(PIT3_IRQ_LEVEL) |      \
88--- 75,105 ----
89  /*
90   * Set up the clock hardware
91   *
92!  * f_pit = f_clk / 2^(preScaleCode+1) / N  = 1/(us_per_tick/us_per_s)
93!  *
94!  * N = f_clk / 2^(preScaleCode+1) * us_per_tick / us_per_s
95!  *
96!  * ns_per_pit_clk = ns_per_s / (f_clk / 2^(preScaleCode+1))
97!  *                = ns_per_s * 2^(preScaleCode+1) / f_clk;
98   */
99  #define Clock_driver_support_initialize_hardware()                       \
100      do {                                                                 \
101+               unsigned long long N;                                            \
102          int level;                                                       \
103!         int preScaleCode = 0;                                            \
104!               N  = bsp_get_CPU_clock_speed();                                  \
105!               N *= rtems_configuration_get_microseconds_per_tick();            \
106!               N /= 2*1000000; /* min_prescale * us_per_s */                    \
107!               while ( N > 0x10000 ) {                                          \
108!                       preScaleCode++;                                              \
109!                       N >>= 1;                                                     \
110!               }                                                                \
111!               PITC_PER_TICK  = N;                                              \
112!               N  = 2000000000ULL << preScaleCode;                              \
113!               N /= bsp_get_CPU_clock_speed();                                  \
114!               NSEC_PER_PITC  = N;                                              \
115!         IDLE_COUNTER   = 0;                                              \
116!         FILTERED_IDLE  = 0;                                              \
117          MAX_IDLE_COUNT = 0;                                              \
118          bsp_allocate_interrupt(PIT3_IRQ_LEVEL, PIT3_IRQ_PRIORITY);       \
119          MCF5282_INTC0_ICR58 = MCF5282_INTC_ICR_IL(PIT3_IRQ_LEVEL) |      \
120***************
121*** 101,108 ****
122                              MCF5282_PIT_PCSR_OVW |                       \
123                              MCF5282_PIT_PCSR_PIE |                       \
124                              MCF5282_PIT_PCSR_RLD;                        \
125!         USEC_PER_TICK = rtems_configuration_get_microseconds_per_tick(); \
126!         MCF5282_PIT3_PMR = USEC_PER_TICK - 1;                            \
127          MCF5282_PIT3_PCSR = MCF5282_PIT_PCSR_PRE(preScaleCode) |         \
128                              MCF5282_PIT_PCSR_PIE |                       \
129                              MCF5282_PIT_PCSR_RLD |                       \
130--- 112,118 ----
131                              MCF5282_PIT_PCSR_OVW |                       \
132                              MCF5282_PIT_PCSR_PIE |                       \
133                              MCF5282_PIT_PCSR_RLD;                        \
134!         MCF5282_PIT3_PMR = PITC_PER_TICK - 1;                            \
135          MCF5282_PIT3_PCSR = MCF5282_PIT_PCSR_PRE(preScaleCode) |         \
136                              MCF5282_PIT_PCSR_PIE |                       \
137                              MCF5282_PIT_PCSR_RLD |                       \
138***************
139*** 115,121 ****
140  Thread bsp_idle_thread(uint32_t ignored)
141  {
142      for(;;)
143!         asm volatile ("addq.l #1,__SRAMBASE"); /* Atomic increment */
144  }
145 
146  int rtems_bsp_cpu_load_percentage(void)
147--- 125,131 ----
148  Thread bsp_idle_thread(uint32_t ignored)
149  {
150      for(;;)
151!         asm volatile ("addq.l #1,%0"::"m"(IDLE_COUNTER)); /* Atomic increment */
152  }
153 
154  int rtems_bsp_cpu_load_percentage(void)
155Index: c/src/lib/libbsp/m68k/uC5282/include/bsp.h
156===================================================================
157RCS file: /usr1/CVS/rtems/c/src/lib/libbsp/m68k/uC5282/include/bsp.h,v
158retrieving revision 1.25
159diff -c -r1.25 bsp.h
160*** c/src/lib/libbsp/m68k/uC5282/include/bsp.h  29 Nov 2009 14:59:41 -0000      1.25
161--- c/src/lib/libbsp/m68k/uC5282/include/bsp.h  22 Feb 2011 18:09:34 -0000
162***************
163*** 134,139 ****
164--- 134,164 ----
165  void *bsp_idle_thread( uintptr_t ignored );
166  #define BSP_IDLE_TASK_BODY bsp_idle_thread
167 
168+ /*
169+  * SRAM. The BSP uses SRAM for maintaining some clock-driver data
170+  *       and for ethernet descriptors (and the initial stack during
171+  *       early boot).
172+  */
173+
174+ typedef struct mcf5282BufferDescriptor_ {
175+     volatile uint16_t   status;
176+     uint16_t                  length;
177+     volatile void      *buffer;
178+ } mcf5282BufferDescriptor_t;
179+
180+ extern struct {
181+       uint32_t                  idle_counter;
182+       uint32_t                  filtered_idle;
183+       uint32_t                  max_idle_count;
184+       uint32_t                  pitc_per_tick;
185+       uint32_t                  nsec_per_pitc;
186+       uint32_t                  pad[3]; /* align to 16-bytes for descriptors */
187+       mcf5282BufferDescriptor_t fec_descriptors[];
188+       /* buffer descriptors are allocated from here */
189+
190+     /* initial stack is at top of SRAM (start.S)  */
191+ } __SRAMBASE;
192+
193  #ifdef __cplusplus
194  }
195  #endif
196Index: c/src/lib/libbsp/m68k/uC5282/network/network.c
197===================================================================
198RCS file: /usr1/CVS/rtems/c/src/lib/libbsp/m68k/uC5282/network/network.c,v
199retrieving revision 1.30
200diff -c -r1.30 network.c
201*** c/src/lib/libbsp/m68k/uC5282/network/network.c      27 Apr 2010 18:23:44 -0000      1.30
202--- c/src/lib/libbsp/m68k/uC5282/network/network.c      22 Feb 2011 18:09:35 -0000
203***************
204*** 81,92 ****
205      #error "Driver must have MCLBYTES > RBUF_SIZE"
206  #endif
207 
208- typedef struct mcf5282BufferDescriptor_ {
209-     volatile uint16_t   status;
210-     uint16_t                  length;
211-     volatile void      *buffer;
212- } mcf5282BufferDescriptor_t;
213-
214  /*
215   * Per-device data
216   */
217--- 81,86 ----
218***************
219*** 197,207 ****
220   * Ensure 128-bit (16-byte) alignment
221   * Allow some space at the beginning for other diagnostic counters
222   */
223- extern char __SRAMBASE[];
224  static mcf5282BufferDescriptor_t *
225  mcf5282_bd_allocate(unsigned int count)
226  {
227!     static mcf5282BufferDescriptor_t *bdp = (mcf5282BufferDescriptor_t *)(__SRAMBASE+16);
228      mcf5282BufferDescriptor_t *p = bdp;
229 
230      bdp += count;
231--- 191,200 ----
232   * Ensure 128-bit (16-byte) alignment
233   * Allow some space at the beginning for other diagnostic counters
234   */
235  static mcf5282BufferDescriptor_t *
236  mcf5282_bd_allocate(unsigned int count)
237  {
238!     static mcf5282BufferDescriptor_t *bdp = __SRAMBASE.fec_descriptors;
239      mcf5282BufferDescriptor_t *p = bdp;
240 
241      bdp += count;