source: rtems/c/src/lib/libcpu/powerpc/ppc403/clock/clock.c @ f9acc33

4.115
Last change on this file since f9acc33 was f9acc33, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/11/11 at 09:46:53

2011-02-11 Ralf Corsépius <ralf.corsepius@…>

  • e500/mmu/mmu.c, mpc505/ictrl/ictrl.c, mpc505/timer/timer.c, mpc5xx/ictrl/ictrl.c, mpc5xx/timer/timer.c, mpc6xx/altivec/vec_sup.c, mpc6xx/clock/c_clock.c, mpc6xx/mmu/bat.c, mpc6xx/mmu/bat.h, mpc6xx/mmu/pte121.c, mpc8260/timer/timer.c, mpc8xx/timer/timer.c, new-exceptions/cpu.c, new-exceptions/bspsupport/ppc_exc_initialize.c, ppc403/clock/clock.c, ppc403/console/console.c, ppc403/console/console.c.polled, ppc403/console/console405.c, ppc403/irq/ictrl.c, ppc403/tty_drv/tty_drv.c, rtems/powerpc/cache.h, shared/include/powerpc-utility.h, shared/src/cache.c: Use "asm" instead of "asm" for improved c99-compliance.
  • Property mode set to 100644
File size: 9.2 KB
RevLine 
[3235ad9]1/*  clock.c
2 *
3 *  This routine initializes the interval timer on the
4 *  PowerPC 403 CPU.  The tick frequency is specified by the bsp.
5 *
[e57b0e2]6 *  Author: Andrew Bray <andy@i-cubed.co.uk>
[3235ad9]7 *
8 *  COPYRIGHT (c) 1995 by i-cubed ltd.
9 *
10 *  To anyone who acknowledges that this file is provided "AS IS"
11 *  without any express or implied warranty:
12 *      permission to use, copy, modify, and distribute this file
13 *      for any purpose is hereby granted without fee, provided that
14 *      the above copyright notice and this notice appears in all
15 *      copies, and that the name of i-cubed limited not be used in
16 *      advertising or publicity pertaining to distribution of the
17 *      software without specific, written prior permission.
18 *      i-cubed limited makes no representations about the suitability
19 *      of this software for any purpose.
20 *
[3ec7bfc]21 *  Derived from c/src/lib/libcpu/hppa1.1/clock/clock.c:
[3235ad9]22 *
[aecfa2b]23 *  Modifications for deriving timer clock from cpu system clock by
24 *              Thomas Doerfler <td@imd.m.isar.de>
25 *  for these modifications:
26 *  COPYRIGHT (c) 1997 by IMD, Puchheim, Germany.
27 *
[c4cc8199]28 *  COPYRIGHT (c) 1989-2007.
[3235ad9]29 *  On-Line Applications Research Corporation (OAR).
30 *
[98e4ebf5]31 *  The license and distribution terms for this file may be
32 *  found in the file LICENSE in this distribution or at
[21e1c44]33 *  http://www.rtems.com/license/LICENSE.
[3235ad9]34 *
[e9ae97fb]35 *  Modifications for PPC405GP by Dennis Ehlin
36 *
[879a047]37 *  $Id$
[3235ad9]38 */
39
[f817b02]40#include <rtems.h>
[11c2382]41#include <rtems/clockdrv.h>
[3a4ae6c]42#include <rtems/libio.h>
[3235ad9]43#include <stdlib.h>                     /* for atexit() */
[73cdeb6]44#include <rtems/bspIo.h>
[e1df032]45#include <rtems/powerpc/powerpc.h>
[b7d1f290]46
[73cdeb6]47/*
48 * check, which exception handling code is present
49 */
[b7d1f290]50
51#include <bsp.h>
52
53#include <bsp/vectors.h>
[73cdeb6]54#include <bsp/irq.h>
[fbd06a09]55
56extern uint32_t bsp_clicks_per_usec;
57extern bool bsp_timer_internal_clock;
[3235ad9]58
[66c373bf]59volatile uint32_t   Clock_driver_ticks;
60static uint32_t   pit_value, tick_time;
[9a73f421]61static bool auto_restart;
[3235ad9]62
[3a4ae6c]63void Clock_exit( void );
[359e537]64
[2247a69]65rtems_isr_entry set_vector(                    /* returns old vector */
66  rtems_isr_entry     handler,                  /* isr routine        */
67  rtems_vector_number vector,                   /* vector number      */
68  int                 type                      /* RTEMS or RAW intr  */
69);
70
[66c373bf]71static inline uint32_t   get_itimer(void)
[3235ad9]72{
[66c373bf]73    register uint32_t   rc;
[3235ad9]74
[e9ae97fb]75#ifndef ppc405 /* this is a ppc403 */
[f9acc33]76    __asm__ volatile ("mfspr %0, 0x3dd" : "=r" ((rc))); /* TBLO */
[e9ae97fb]77#else /* ppc405 */
[f9acc33]78    __asm__ volatile ("mfspr %0, 0x10c" : "=r" ((rc))); /* 405GP TBL */
[e9ae97fb]79#endif /* ppc405 */
[3235ad9]80
[3a4ae6c]81    return rc;
82}
83
84/*
85 *  ISR Handler
86 */
[73cdeb6]87
88void Clock_isr(void* handle)
[3235ad9]89{
[73cdeb6]90    uint32_t   clicks_til_next_interrupt;
[b7d1f290]91#if defined(BSP_PPC403_CLOCK_ISR_IRQ_LEVEL)
92        uint32_t   l_orig = _ISR_Get_level();
93#endif
[3a4ae6c]94    if (!auto_restart)
[e57b0e2]95    {
[66c373bf]96      uint32_t   itimer_value;
[e57b0e2]97      /*
98       * setup for next interrupt; making sure the new value is reasonably
99       * in the future.... in case we lost out on an interrupt somehow
100       */
[359e537]101
[e57b0e2]102      itimer_value = get_itimer();
103      tick_time += pit_value;
[359e537]104
[e57b0e2]105      /*
106       * how far away is next interrupt *really*
107       * It may be a long time; this subtraction works even if
108       * Clock_clicks_interrupt < Clock_clicks_low_order via
109       * the miracle of unsigned math.
110       */
111      clicks_til_next_interrupt = tick_time - itimer_value;
[359e537]112
[e57b0e2]113      /*
114       * If it is too soon then bump it up.
115       * This should only happen if CPU_HPPA_CLICKS_PER_TICK is too small.
116       * But setting it low is useful for debug, so...
117       */
[359e537]118
[e57b0e2]119      if (clicks_til_next_interrupt < 400)
120      {
121        tick_time = itimer_value + 1000;
122        clicks_til_next_interrupt = 1000;
123        /* XXX: count these! this should be rare */
124      }
[359e537]125
[e57b0e2]126      /*
127       * If it is too late, that means we missed the interrupt somehow.
128       * Rather than wait 35-50s for a wrap, we just fudge it here.
129       */
[359e537]130
[e57b0e2]131      if (clicks_til_next_interrupt > pit_value)
132      {
133        tick_time = itimer_value + 1000;
134        clicks_til_next_interrupt = 1000;
135        /* XXX: count these! this should never happen :-) */
136      }
[359e537]137
[f9acc33]138      __asm__ volatile ("mtspr 0x3db, %0" :: "r"
[e57b0e2]139                         (clicks_til_next_interrupt)); /* PIT */
140  }
[359e537]141
[f9acc33]142    __asm__ volatile ( "mtspr 0x3d8, %0" :: "r" (0x08000000)); /* TSR */
[359e537]143
[3a4ae6c]144    Clock_driver_ticks++;
[b7d1f290]145
146        /* Give BSP a chance to say if they want to re-enable interrupts */
[359e537]147
[b7d1f290]148#if defined(BSP_PPC403_CLOCK_ISR_IRQ_LEVEL)
149        _ISR_Set_level(BSP_PPC403_CLOCK_ISR_IRQ_LEVEL);
150#endif
151
[3a4ae6c]152    rtems_clock_tick();
[b7d1f290]153
154#if defined(BSP_PPC403_CLOCK_ISR_IRQ_LEVEL)
155        _ISR_Set_level(l_orig)
156#endif
[3235ad9]157}
158
[73cdeb6]159int ClockIsOn(const rtems_irq_connect_data* unused)
160{
161    register uint32_t   tcr;
[359e537]162
[f9acc33]163    __asm__ volatile ("mfspr %0, 0x3da" : "=r" ((tcr))); /* TCR */
[359e537]164
[73cdeb6]165    return (tcr & 0x04000000) != 0;
166}
167
168void ClockOff(
169              const rtems_irq_connect_data* unused
[fbd06a09]170)
[73cdeb6]171{
172    register uint32_t   tcr;
[359e537]173
[f9acc33]174    __asm__ volatile ("mfspr %0, 0x3da" : "=r" ((tcr))); /* TCR */
[359e537]175
[73cdeb6]176    tcr &= ~ 0x04400000;
[359e537]177
[f9acc33]178    __asm__ volatile ("mtspr 0x3da, %0" : "=r" ((tcr)) : "0" ((tcr))); /* TCR */
[73cdeb6]179}
180
181void ClockOn(
182              const rtems_irq_connect_data* unused
[fbd06a09]183)
[3235ad9]184{
[66c373bf]185    uint32_t   iocr;
186    register uint32_t   tcr;
[e9ae97fb]187#ifdef ppc403
[66c373bf]188    uint32_t   pvr;
[e9ae97fb]189#endif /* ppc403 */
[359e537]190
[3235ad9]191    Clock_driver_ticks = 0;
[359e537]192
[e9ae97fb]193#ifndef ppc405 /* this is a ppc403 */
[f9acc33]194    __asm__ volatile ("mfdcr %0, 0xa0" : "=r" (iocr)); /* IOCR */
[c4cc8199]195    if (bsp_timer_internal_clock) {
[aecfa2b]196        iocr &= ~4; /* timer clocked from system clock */
197    }
198    else {
199        iocr |= 4; /* select external timer clock */
200    }
[f9acc33]201    __asm__ volatile ("mtdcr 0xa0, %0" : "=r" (iocr) : "0" (iocr)); /* IOCR */
[359e537]202
[f9acc33]203    __asm__ volatile ("mfspr %0, 0x11f" : "=r" ((pvr))); /* PVR */
[3235ad9]204    if (((pvr & 0xffff0000) >> 16) != 0x0020)
[e57b0e2]205      return; /* Not a ppc403 */
[359e537]206
[3235ad9]207    if ((pvr & 0xff00) == 0x0000) /* 403GA */
[aecfa2b]208#if 0 /* FIXME: in which processor versions will "autoload" work properly? */
[9a73f421]209      auto_restart = (pvr & 0x00f0) > 0x0000 ? true : false;
[359e537]210#else
[aecfa2b]211    /* no known chip version supports auto restart of timer... */
[9a73f421]212    auto_restart = false;
[aecfa2b]213#endif
[3235ad9]214    else if ((pvr & 0xff00) == 0x0100) /* 403GB */
[9a73f421]215      auto_restart = true;
[359e537]216
[e9ae97fb]217#else /* ppc405 */
[f9acc33]218    __asm__ volatile ("mfdcr %0, 0x0b2" : "=r" (iocr));  /*405GP CPC0_CR1 */
[c4cc8199]219    if (bsp_timer_internal_clock) {
[e9ae97fb]220        iocr &=~0x800000        ;/* timer clocked from system clock CETE*/
221    }
222    else {
223        iocr |= 0x800000; /* select external timer clock CETE*/
224    }
[f9acc33]225    __asm__ volatile ("mtdcr 0x0b2, %0" : "=r" (iocr) : "0" (iocr)); /* 405GP CPC0_CR1 */
[e9ae97fb]226
227     /*
228      * Enable auto restart
229      */
230
[9a73f421]231    auto_restart=true;
[e9ae97fb]232
233#endif /* ppc405 */
[f817b02]234    pit_value = rtems_configuration_get_microseconds_per_tick() *
[c4cc8199]235      bsp_clicks_per_usec;
[359e537]236
[e9ae97fb]237     /*
238      * Set PIT value
239      */
240
[f9acc33]241    __asm__ volatile ("mtspr 0x3db, %0" : : "r" (pit_value)); /* PIT */
[359e537]242
243     /*
[e9ae97fb]244      * Set timer to autoreload, bit TCR->ARE = 1  0x0400000
245      * Enable PIT interrupt, bit TCR->PIE = 1     0x4000000
246      */
[0dd1d44]247    tick_time = get_itimer() + pit_value;
[73cdeb6]248
[f9acc33]249    __asm__ volatile ("mfspr %0, 0x3da" : "=r" ((tcr))); /* TCR */
[e9ae97fb]250    tcr = (tcr & ~0x04400000) | (auto_restart ? 0x04400000 : 0x04000000);
[73cdeb6]251#if 1
[f9acc33]252    __asm__ volatile ("mtspr 0x3da, %0" : "=r" ((tcr)) : "0" ((tcr))); /* TCR */
[73cdeb6]253#endif
[0dd1d44]254
[3235ad9]255}
256
[73cdeb6]257
258
259void Install_clock(
[fbd06a09]260  void (*clock_isr)(void *)
261)
[3235ad9]262{
[fbd06a09]263   rtems_irq_connect_data clockIrqConnData;
264
[73cdeb6]265#ifdef ppc403
266    uint32_t   pvr;
267#endif /* ppc403 */
[359e537]268
[73cdeb6]269    Clock_driver_ticks = 0;
[359e537]270
[73cdeb6]271    /*
272     * initialize the interval here
273     * First tick is set to right amount of time in the future
274     * Future ticks will be incremented over last value set
275     * in order to provide consistent clicks in the face of
276     * interrupt overhead
277     */
278
279   clockIrqConnData.on   = ClockOn;
280   clockIrqConnData.off  = ClockOff;
281   clockIrqConnData.isOn = ClockIsOn;
282   clockIrqConnData.name = BSP_PIT;
283   clockIrqConnData.hdl  = clock_isr;
284   if (!BSP_install_rtems_irq_handler (&clockIrqConnData)) {
285     printk("Unable to connect Clock Irq handler\n");
286     rtems_fatal_error_occurred(1);
287   }
[fbd06a09]288
289   atexit(Clock_exit);
[73cdeb6]290}
[3235ad9]291
[73cdeb6]292void
293ReInstall_clock(
[fbd06a09]294  void (*new_clock_isr)(void *)
[73cdeb6]295)
296{
297  uint32_t   isrlevel = 0;
[fbd06a09]298  rtems_irq_connect_data clockIrqConnData;
[359e537]299
[73cdeb6]300  rtems_interrupt_disable(isrlevel);
[359e537]301
302
[fbd06a09]303  clockIrqConnData.name = BSP_PIT;
304  if (!BSP_get_current_rtems_irq_handler(&clockIrqConnData)) {
305    printk("Unable to stop system clock\n");
306    rtems_fatal_error_occurred(1);
307  }
[359e537]308
[fbd06a09]309  BSP_remove_rtems_irq_handler (&clockIrqConnData);
[359e537]310
[fbd06a09]311  clockIrqConnData.on   = ClockOn;
312  clockIrqConnData.off  = ClockOff;
313  clockIrqConnData.isOn = ClockIsOn;
314  clockIrqConnData.name = BSP_PIT;
315  clockIrqConnData.hdl  = new_clock_isr;
[3235ad9]316
[fbd06a09]317  if (!BSP_install_rtems_irq_handler (&clockIrqConnData)) {
318    printk("Unable to connect Clock Irq handler\n");
319    rtems_fatal_error_occurred(1);
[73cdeb6]320  }
321
322  rtems_interrupt_enable(isrlevel);
[3235ad9]323}
324
[3a4ae6c]325
[3235ad9]326/*
327 * Called via atexit()
328 * Remove the clock interrupt handler by setting handler to NULL
[e9ae97fb]329 *
[359e537]330 * This will not work on the 405GP because
331 * when bit's are set in TCR they can only be unset by a reset
[3235ad9]332 */
333
[73cdeb6]334void Clock_exit(void)
[3235ad9]335{
[fbd06a09]336  rtems_irq_connect_data clockIrqConnData;
[359e537]337
[fbd06a09]338  clockIrqConnData.name = BSP_PIT;
339  if (!BSP_get_current_rtems_irq_handler(&clockIrqConnData)) {
340    printk("Unable to stop system clock\n");
341    rtems_fatal_error_occurred(1);
342  }
[359e537]343
[fbd06a09]344  BSP_remove_rtems_irq_handler (&clockIrqConnData);
[3235ad9]345}
346
[3a4ae6c]347rtems_device_driver Clock_initialize(
348  rtems_device_major_number major,
349  rtems_device_minor_number minor,
350  void *pargp
351)
352{
353  Install_clock( Clock_isr );
[359e537]354
[3a4ae6c]355  return RTEMS_SUCCESSFUL;
356}
Note: See TracBrowser for help on using the repository browser.