rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <debug.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <err.h> |
| 26 | #include <stdio.h> |
| 27 | #include <assert.h> |
| 28 | #include <trace.h> |
| 29 | #include <kernel/thread.h> |
| 30 | #include <platform.h> |
| 31 | #include <platform/interrupts.h> |
| 32 | #include <platform/timer.h> |
| 33 | #include <platform/zynq.h> |
| 34 | #include "platform_p.h" |
| 35 | |
| 36 | /* unused, arm_cortex_a9_timer does timer duty */ |
| 37 | |
| 38 | #if 0 |
| 39 | /* driver for Cadence triple timer counter (TTC) */ |
| 40 | |
| 41 | #define LOCAL_TRACE 0 |
| 42 | |
| 43 | #define TIMREG(reg) (*REG32(TTC0_BASE + (reg))) |
| 44 | #define TIMREG16(reg) (*REG16(TTC0_BASE + (reg))) |
| 45 | #define TIMREG8(reg) (*REG8(TTC0_BASE + (reg))) |
| 46 | |
| 47 | #define CLK_CTRL(n) (0x00 + (n) * 4) |
| 48 | #define CNT_CTRL(n) (0x0c + (n) * 4) |
| 49 | #define CNT_VAL(n) (0x18 + (n) * 4) |
| 50 | #define INTERVAL_VAL(n) (0x24 + (n) * 4) |
| 51 | #define MATCH_1(n) (0x30 + (n) * 4) |
| 52 | #define MATCH_2(n) (0x3c + (n) * 4) |
| 53 | #define MATCH_3(n) (0x48 + (n) * 4) |
| 54 | #define ISR(n) (0x54 + (n) * 4) |
| 55 | #define IEN(n) (0x60 + (n) * 4) |
| 56 | #define EVT_CTRL(n) (0x6c + (n) * 4) |
| 57 | #define EVT(n) (0x78 + (n) * 4) |
| 58 | |
| 59 | static platform_timer_callback t_callback; |
| 60 | |
| 61 | static volatile uint ticks = 0; |
| 62 | static lk_time_t periodic_interval; |
| 63 | |
| 64 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval) |
| 65 | { |
| 66 | enter_critical_section(); |
| 67 | |
| 68 | LTRACEF("callback %p, arg %p, interval %lu\n", callback, arg, interval); |
| 69 | |
| 70 | t_callback = callback; |
| 71 | |
| 72 | periodic_interval = interval; |
| 73 | |
| 74 | uint32_t ticks = periodic_interval * 1000; /* timer is running close to 1Mhz */ |
| 75 | ASSERT(ticks <= 0xffff); |
| 76 | |
| 77 | TIMREG(IEN(0)) = (1<<0); // interval interrupt |
| 78 | TIMREG(INTERVAL_VAL(0)) = ticks; |
| 79 | TIMREG(CNT_CTRL(0)) = (1<<5) | (1<<4) | (1<<1); // no wave, reset, interval mode |
| 80 | |
| 81 | unmask_interrupt(TTC0_A_INT); |
| 82 | |
| 83 | exit_critical_section(); |
| 84 | |
| 85 | return NO_ERROR; |
| 86 | } |
| 87 | |
| 88 | lk_bigtime_t current_time_hires(void) |
| 89 | { |
| 90 | lk_bigtime_t time; |
| 91 | |
| 92 | time = ticks * periodic_interval * 1000ULL; |
| 93 | |
| 94 | return time; |
| 95 | } |
| 96 | |
| 97 | lk_time_t current_time(void) |
| 98 | { |
| 99 | lk_time_t time; |
| 100 | |
| 101 | time = ticks * periodic_interval; |
| 102 | |
| 103 | return time; |
| 104 | } |
| 105 | |
| 106 | static enum handler_return platform_tick(void *arg) |
| 107 | { |
| 108 | ticks++; |
| 109 | |
| 110 | volatile uint32_t hole = TIMREG(ISR(0)); // ack the irq |
| 111 | |
| 112 | if (t_callback) { |
| 113 | return t_callback(arg, current_time()); |
| 114 | } else { |
| 115 | return INT_NO_RESCHEDULE; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void platform_init_timer(void) |
| 120 | { |
| 121 | /* disable timers */ |
| 122 | TIMREG(CNT_CTRL(0)) = 0x1; |
| 123 | TIMREG(CNT_CTRL(1)) = 0x1; |
| 124 | TIMREG(CNT_CTRL(2)) = 0x1; |
| 125 | |
| 126 | TIMREG(CLK_CTRL(0)) = (6 << 1) | 1; // prescale 133Mhz/(2^7) == 1039062Hz (close to 1Mhz) |
| 127 | |
| 128 | register_int_handler(TTC0_A_INT, &platform_tick, NULL); |
| 129 | register_int_handler(TTC0_B_INT, &platform_tick, NULL); |
| 130 | register_int_handler(TTC0_C_INT, &platform_tick, NULL); |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | /* vim: set ts=4 sw=4 expandtab: */ |