[98eb7e78] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup bsp_clock |
---|
| 5 | * |
---|
| 6 | * @brief Raspberry Pi clock support. |
---|
| 7 | */ |
---|
| 8 | |
---|
[c32b1ef] | 9 | /* |
---|
| 10 | * BCM2835 Clock driver |
---|
| 11 | * |
---|
| 12 | * Copyright (c) 2013 Alan Cudmore |
---|
[5d369c85] | 13 | * Copyright (c) 2016 Pavel Pisa |
---|
[c32b1ef] | 14 | * |
---|
| 15 | * The license and distribution terms for this file may be |
---|
| 16 | * found in the file LICENSE in this distribution or at |
---|
| 17 | * |
---|
[c499856] | 18 | * http://www.rtems.org/license/LICENSE |
---|
[c32b1ef] | 19 | * |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <rtems.h> |
---|
| 23 | #include <bsp.h> |
---|
| 24 | #include <bsp/irq.h> |
---|
[5d369c85] | 25 | #include <bsp/irq-generic.h> |
---|
[c32b1ef] | 26 | #include <bsp/raspberrypi.h> |
---|
[5d369c85] | 27 | #include <rtems/timecounter.h> |
---|
[c32b1ef] | 28 | |
---|
[7632906] | 29 | /* This is defined in ../../../shared/dev/clock/clockimpl.h */ |
---|
[c32b1ef] | 30 | void Clock_isr(rtems_irq_hdl_param arg); |
---|
| 31 | |
---|
[5d369c85] | 32 | static struct timecounter raspberrypi_tc; |
---|
| 33 | |
---|
| 34 | static uint32_t raspberrypi_clock_get_timecount(struct timecounter *tc) |
---|
| 35 | { |
---|
| 36 | return BCM2835_REG(BCM2835_GPU_TIMER_CLO); |
---|
| 37 | } |
---|
| 38 | |
---|
[c32b1ef] | 39 | static void raspberrypi_clock_at_tick(void) |
---|
| 40 | { |
---|
[5d369c85] | 41 | uint32_t act_val; |
---|
| 42 | uint32_t next_cmp = BCM2835_REG(BCM2835_GPU_TIMER_C3); |
---|
| 43 | next_cmp += rtems_configuration_get_microseconds_per_tick(); |
---|
| 44 | BCM2835_REG(BCM2835_GPU_TIMER_C3) = next_cmp; |
---|
| 45 | act_val = BCM2835_REG(BCM2835_GPU_TIMER_CLO); |
---|
| 46 | |
---|
| 47 | /* |
---|
| 48 | * Clear interrupt only if there is time left to the next tick. |
---|
| 49 | * If time of the next tick has already passed then interrupt |
---|
| 50 | * request stays active and fires immediately after current tick |
---|
| 51 | * processing is finished. |
---|
| 52 | */ |
---|
| 53 | if ((int32_t)(next_cmp - act_val) > 0) |
---|
| 54 | BCM2835_REG(BCM2835_GPU_TIMER_CS) = BCM2835_GPU_TIMER_CS_M3; |
---|
[c32b1ef] | 55 | } |
---|
| 56 | |
---|
[5d369c85] | 57 | static void raspberrypi_clock_handler_install_isr( |
---|
| 58 | rtems_isr_entry clock_isr |
---|
| 59 | ) |
---|
[c32b1ef] | 60 | { |
---|
| 61 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 62 | |
---|
[5d369c85] | 63 | if (clock_isr != NULL) { |
---|
| 64 | sc = rtems_interrupt_handler_install( |
---|
| 65 | BCM2835_IRQ_ID_GPU_TIMER_M3, |
---|
| 66 | "Clock", |
---|
| 67 | RTEMS_INTERRUPT_UNIQUE, |
---|
| 68 | (rtems_interrupt_handler) clock_isr, |
---|
| 69 | NULL |
---|
| 70 | ); |
---|
| 71 | } else { |
---|
| 72 | /* Remove interrupt handler */ |
---|
| 73 | sc = rtems_interrupt_handler_remove( |
---|
| 74 | BCM2835_IRQ_ID_GPU_TIMER_M3, |
---|
| 75 | (rtems_interrupt_handler) Clock_isr, |
---|
| 76 | NULL |
---|
| 77 | ); |
---|
| 78 | } |
---|
| 79 | if ( sc != RTEMS_SUCCESSFUL ) { |
---|
[c32b1ef] | 80 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
[5d369c85] | 84 | static void raspberrypi_clock_initialize_hardware(void) |
---|
[c32b1ef] | 85 | { |
---|
[5d369c85] | 86 | uint32_t next_cmp = BCM2835_REG(BCM2835_GPU_TIMER_CLO); |
---|
| 87 | next_cmp += rtems_configuration_get_microseconds_per_tick(); |
---|
| 88 | BCM2835_REG(BCM2835_GPU_TIMER_C3) = next_cmp; |
---|
| 89 | BCM2835_REG(BCM2835_GPU_TIMER_CS) = BCM2835_GPU_TIMER_CS_M3; |
---|
| 90 | |
---|
| 91 | raspberrypi_tc.tc_get_timecount = raspberrypi_clock_get_timecount; |
---|
| 92 | raspberrypi_tc.tc_counter_mask = 0xffffffff; |
---|
| 93 | raspberrypi_tc.tc_frequency = 1000000; /* 1 MHz */ |
---|
| 94 | raspberrypi_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
| 95 | rtems_timecounter_install(&raspberrypi_tc); |
---|
[c32b1ef] | 96 | } |
---|
| 97 | |
---|
| 98 | static void raspberrypi_clock_cleanup(void) |
---|
| 99 | { |
---|
[5d369c85] | 100 | bsp_interrupt_vector_disable(BCM2835_IRQ_ID_GPU_TIMER_M3); |
---|
[c32b1ef] | 101 | } |
---|
| 102 | |
---|
| 103 | #define Clock_driver_support_at_tick() raspberrypi_clock_at_tick() |
---|
| 104 | |
---|
[5d369c85] | 105 | #define Clock_driver_support_initialize_hardware() raspberrypi_clock_initialize_hardware() |
---|
[c32b1ef] | 106 | |
---|
[5d369c85] | 107 | #define Clock_driver_support_shutdown_hardware() raspberrypi_clock_cleanup() |
---|
| 108 | |
---|
[f3b29236] | 109 | #define Clock_driver_support_install_isr(clock_isr) \ |
---|
| 110 | raspberrypi_clock_handler_install_isr(clock_isr) |
---|
[c32b1ef] | 111 | |
---|
[5d369c85] | 112 | #define CLOCK_DRIVER_USE_ONLY_BOOT_PROCESSOR 1 |
---|
[c32b1ef] | 113 | |
---|
[7632906] | 114 | #include "../../../shared/dev/clock/clockimpl.h" |
---|