1 | /* |
---|
2 | * Copyright (c) 2013 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <info@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.com/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <bsp.h> |
---|
16 | #include <bsp/irq.h> |
---|
17 | #include <bsp/arm-a9mpcore-regs.h> |
---|
18 | #include <bsp/arm-a9mpcore-clock.h> |
---|
19 | |
---|
20 | #define A9MPCORE_PT ((volatile a9mpcore_pt *) BSP_ARM_A9MPCORE_PT_BASE) |
---|
21 | |
---|
22 | static uint64_t a9mpcore_clock_last_tick_k; |
---|
23 | |
---|
24 | /* This is defined in clockdrv_shell.h */ |
---|
25 | void Clock_isr(rtems_irq_hdl_param arg); |
---|
26 | |
---|
27 | __attribute__ ((weak)) uint32_t a9mpcore_clock_periphclk(void) |
---|
28 | { |
---|
29 | /* default to the BSP option. */ |
---|
30 | return BSP_ARM_A9MPCORE_PERIPHCLK; |
---|
31 | } |
---|
32 | |
---|
33 | static void a9mpcore_clock_at_tick(void) |
---|
34 | { |
---|
35 | volatile a9mpcore_pt *pt = A9MPCORE_PT; |
---|
36 | |
---|
37 | pt->irqst = A9MPCORE_PT_IRQST_EFLG; |
---|
38 | } |
---|
39 | |
---|
40 | static void a9mpcore_clock_handler_install(void) |
---|
41 | { |
---|
42 | rtems_status_code sc; |
---|
43 | |
---|
44 | sc = rtems_interrupt_handler_install( |
---|
45 | A9MPCORE_IRQ_PT, |
---|
46 | "Clock", |
---|
47 | RTEMS_INTERRUPT_UNIQUE, |
---|
48 | (rtems_interrupt_handler) Clock_isr, |
---|
49 | NULL |
---|
50 | ); |
---|
51 | if (sc != RTEMS_SUCCESSFUL) { |
---|
52 | rtems_fatal( |
---|
53 | RTEMS_FATAL_SOURCE_BSP_SPECIFIC, |
---|
54 | BSP_ARM_A9MPCORE_FATAL_CLOCK_IRQ_INSTALL |
---|
55 | ); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | static void a9mpcore_clock_initialize(void) |
---|
60 | { |
---|
61 | volatile a9mpcore_pt *pt = A9MPCORE_PT; |
---|
62 | uint64_t periphclk = (uint64_t) a9mpcore_clock_periphclk(); |
---|
63 | uint64_t interval = (periphclk |
---|
64 | * (uint64_t) rtems_configuration_get_microseconds_per_tick()) / 1000000; |
---|
65 | |
---|
66 | a9mpcore_clock_last_tick_k = (1000000000ULL << 32) / periphclk; |
---|
67 | |
---|
68 | pt->load = (uint32_t) interval - 1; |
---|
69 | pt->ctrl = A9MPCORE_PT_CTRL_AUTO_RLD |
---|
70 | | A9MPCORE_PT_CTRL_IRQ_EN |
---|
71 | | A9MPCORE_PT_CTRL_TMR_EN; |
---|
72 | } |
---|
73 | |
---|
74 | static void a9mpcore_clock_cleanup(void) |
---|
75 | { |
---|
76 | volatile a9mpcore_pt *pt = A9MPCORE_PT; |
---|
77 | rtems_status_code sc; |
---|
78 | |
---|
79 | pt->ctrl = 0; |
---|
80 | pt->irqst = A9MPCORE_PT_IRQST_EFLG; |
---|
81 | |
---|
82 | sc = rtems_interrupt_handler_remove( |
---|
83 | A9MPCORE_IRQ_PT, |
---|
84 | (rtems_interrupt_handler) Clock_isr, |
---|
85 | NULL |
---|
86 | ); |
---|
87 | if (sc != RTEMS_SUCCESSFUL) { |
---|
88 | rtems_fatal( |
---|
89 | RTEMS_FATAL_SOURCE_BSP_SPECIFIC, |
---|
90 | BSP_ARM_A9MPCORE_FATAL_CLOCK_IRQ_REMOVE |
---|
91 | ); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | static uint32_t a9mpcore_clock_nanoseconds_since_last_tick(void) |
---|
96 | { |
---|
97 | volatile a9mpcore_pt *pt = A9MPCORE_PT; |
---|
98 | uint64_t k = a9mpcore_clock_last_tick_k; |
---|
99 | uint32_t c = pt->cntr; |
---|
100 | uint32_t p = pt->load + 1; |
---|
101 | |
---|
102 | if ((pt->irqst & A9MPCORE_PT_IRQST_EFLG) != 0) { |
---|
103 | c = pt->cntr - p; |
---|
104 | } |
---|
105 | |
---|
106 | return (uint32_t) (((p - c) * k) >> 32); |
---|
107 | } |
---|
108 | |
---|
109 | #define Clock_driver_support_at_tick() \ |
---|
110 | a9mpcore_clock_at_tick() |
---|
111 | |
---|
112 | #define Clock_driver_support_initialize_hardware() \ |
---|
113 | a9mpcore_clock_initialize() |
---|
114 | |
---|
115 | #define Clock_driver_support_install_isr(isr, old_isr) \ |
---|
116 | do { \ |
---|
117 | a9mpcore_clock_handler_install(); \ |
---|
118 | old_isr = NULL; \ |
---|
119 | } while (0) |
---|
120 | |
---|
121 | #define Clock_driver_support_shutdown_hardware() \ |
---|
122 | a9mpcore_clock_cleanup() |
---|
123 | |
---|
124 | #define Clock_driver_nanoseconds_since_last_tick \ |
---|
125 | a9mpcore_clock_nanoseconds_since_last_tick |
---|
126 | |
---|
127 | /* Include shared source clock driver code */ |
---|
128 | #include "../../shared/clockdrv_shell.h" |
---|