summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libcpu/arm/mc9328mxl/clock/clockdrv.c
blob: d5dc69c9a0ccc727ffcb004cf4d84b3fe53f2b2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  MC9328MXL clock specific using the System Timer
 */

/*
 *  Copyright (c) 2004 by Cogent Computer Systems
 *  Written by Jay Monkman <jtm@lopingdog.com>
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <rtems.h>
#include <bsp.h>
#include <bsp/irq.h>
#include <mc9328mxl.h>
#include <rtems/bspIo.h>  /* for printk */

/* this is defined in ../../../shared/clockdrv_shell.h */
void Clock_isr(rtems_irq_hdl_param arg);
static void clock_isr_on(const rtems_irq_connect_data *unused);
static void clock_isr_off(const rtems_irq_connect_data *unused);
static int clock_isr_is_on(const rtems_irq_connect_data *irq);

/* Replace the first value with the clock's interrupt name. */
rtems_irq_connect_data clock_isr_data = {
  .name   = BSP_INT_TIMER1,
  .hdl    = Clock_isr,
  .handle = (void *)BSP_INT_TIMER1,
  .on     = clock_isr_on,
  .off    = clock_isr_off,
  .isOn   = clock_isr_is_on,
};

/**
 * When we get the clock interrupt
 *    - clear the interrupt bit?
 *    - restart the timer?
 */
#define Clock_driver_support_at_tick()               \
  do {                                               \
    uint32_t reg;                                    \
                                                     \
    reg = MC9328MXL_TMR1_TSTAT;                      \
    (void) reg; /* avoid set but not used warning */ \
    MC9328MXL_TMR1_TSTAT = 0;                        \
  } while(0)

/**
 * Installs the clock ISR. You shouldn't need to change this.
 */
#define Clock_driver_support_install_isr( _new, _old ) \
  do {                                                 \
      (_old) = NULL;                                   \
      BSP_install_rtems_irq_handler(&clock_isr_data);  \
  } while(0)

/**
 * Initialize the hardware for the clock
 *   - Set the frequency
 *   - enable it
 *   - clear any pending interrupts
 *
 * Since you may want the clock always running, you can
 * enable interrupts here. If you do so, the clock_isr_on(),
 * clock_isr_off(), and clock_isr_is_on() functions can be
 * NOPs.
 */
#define Clock_driver_support_initialize_hardware() \
  do { \
        int freq; \
        int cnt; \
        freq = get_perclk1_freq(); \
        printk("perclk1 freq is %d\n", freq); \
        cnt = ((long long)freq * rtems_configuration_get_microseconds_per_tick() + 500000) / 1000000;\
        printk("cnt freq is %d\n", cnt); \
        MC9328MXL_TMR1_TCMP = cnt; \
        /* use PERCLK1 as input, enable timer */ \
        MC9328MXL_TMR1_TCTL = (MC9328MXL_TMR_TCTL_CLKSRC_PCLK1 | \
                               MC9328MXL_TMR_TCTL_TEN | \
                               MC9328MXL_TMR_TCTL_IRQEN); \
        /* set prescaler to 1 (register value + 1) */ \
        MC9328MXL_TMR1_TPRER = 0; \
     } while (0)

/**
 * Do whatever you need to shut the clock down and remove the
 * interrupt handler. Since this normally only gets called on
 * RTEMS shutdown, you may not need to do anything other than
 * remove the ISR.
 */
#define Clock_driver_support_shutdown_hardware()                        \
  do {                                                                  \
    /* Disable timer */ \
    MC9328MXL_TMR1_TCTL = 0; \
    BSP_remove_rtems_irq_handler(&clock_isr_data);                  \
  } while (0)

/**
 * Enables clock interrupt.
 *
 * If the interrupt is always on, this can be a NOP.
 */
static void clock_isr_on(const rtems_irq_connect_data *unused)
{
  MC9328MXL_TMR1_TCTL |= MC9328MXL_TMR_TCTL_IRQEN;
  MC9328MXL_AITC_INTENNUM = MC9328MXL_INT_TIMER1;
}

/**
 * Disables clock interrupts
 *
 * If the interrupt is always on, this can be a NOP.
 */
static void clock_isr_off(const rtems_irq_connect_data *unused)
{
  MC9328MXL_TMR1_TCTL &= ~MC9328MXL_TMR_TCTL_IRQEN;
  MC9328MXL_AITC_INTDISNUM = MC9328MXL_INT_TIMER1;
}

/**
 * Tests to see if clock interrupt is enabled, and returns 1 if so.
 * If interrupt is not enabled, returns 0.
 *
 * If the interrupt is always on, this always returns 1.
 */
static int clock_isr_is_on(const rtems_irq_connect_data *irq)
{
  return MC9328MXL_TMR1_TCTL & MC9328MXL_TMR_TCTL_IRQEN;
}

#define CLOCK_DRIVER_USE_DUMMY_TIMECOUNTER

/* Make sure to include this, and only at the end of the file */

#include "../../../../libbsp/shared/clockdrv_shell.h"