rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 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 <arch/mips.h> |
| 24 | #include <err.h> |
| 25 | #include <trace.h> |
| 26 | #include <debug.h> |
| 27 | #include <assert.h> |
| 28 | #include <stdint.h> |
| 29 | #include <bits.h> |
| 30 | #include <arch/ops.h> |
| 31 | #include <platform.h> |
| 32 | #include <platform/timer.h> |
| 33 | |
| 34 | #define LOCAL_TRACE 0 |
| 35 | |
| 36 | static volatile uint64_t ticks; |
| 37 | static volatile uint32_t last_compare_set; |
| 38 | |
| 39 | static uint32_t tick_rate; |
| 40 | static uint32_t tick_rate_mhz; |
| 41 | |
| 42 | static lk_time_t tick_interval_ms; |
| 43 | static lk_bigtime_t tick_interval_us; |
| 44 | static uint32_t tick_interval; |
| 45 | |
| 46 | static platform_timer_callback cb; |
| 47 | static void *cb_args; |
| 48 | |
| 49 | enum handler_return mips_timer_irq(void) |
| 50 | { |
| 51 | LTRACEF("count 0x%x\n", mips_read_c0_count()); |
| 52 | LTRACEF("compare 0x%x\n", mips_read_c0_compare()); |
| 53 | |
| 54 | /* reset it for the next interval */ |
| 55 | retry: |
| 56 | ticks++; |
| 57 | last_compare_set += tick_interval; |
| 58 | uint32_t count = mips_read_c0_count(); |
| 59 | if (unlikely(TIME_GT(count, last_compare_set))) { |
| 60 | /* if it took us too long to get to this irq, make sure it fires immediately */ |
| 61 | //printf("took too long to service timer irq! %u %u\n", count, last_compare_set); |
| 62 | goto retry; |
| 63 | //mips_write_c0_compare(mips_read_c0_count() + tick_rate_mhz); |
| 64 | } else { |
| 65 | mips_write_c0_compare(last_compare_set); |
| 66 | } |
| 67 | |
| 68 | enum handler_return ret = INT_NO_RESCHEDULE; |
| 69 | if (cb) { |
| 70 | lk_time_t now = current_time(); |
| 71 | ret = cb(cb_args, now); |
| 72 | } |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval) |
| 78 | { |
| 79 | TRACEF("callback %p, arg %p, interval %u\n", callback, arg, interval); |
| 80 | |
| 81 | DEBUG_ASSERT(interval > 0); |
| 82 | DEBUG_ASSERT(tick_rate != 0 && tick_rate_mhz != 0); |
| 83 | |
| 84 | cb = callback; |
| 85 | cb_args = arg; |
| 86 | |
| 87 | tick_interval_ms = interval; |
| 88 | tick_interval_us = interval * 1000; |
| 89 | tick_interval = interval * (tick_rate / 1000); |
| 90 | |
| 91 | uint32_t now = mips_read_c0_count(); |
| 92 | last_compare_set = now + tick_interval; |
| 93 | mips_write_c0_compare(last_compare_set); |
| 94 | |
| 95 | // enable the counter |
| 96 | mips_write_c0_cause(mips_read_c0_cause() & ~(1<<27)); |
| 97 | |
| 98 | return NO_ERROR; |
| 99 | } |
| 100 | |
| 101 | lk_time_t current_time(void) |
| 102 | { |
| 103 | uint64_t t; |
| 104 | uint32_t last_compare; |
| 105 | uint32_t delta; |
| 106 | |
| 107 | /* sample the tick counter, the last compare register set, and the current count atomically */ |
| 108 | do { |
| 109 | t = ticks; |
| 110 | last_compare = last_compare_set; |
| 111 | delta = mips_read_c0_count(); |
| 112 | } while (ticks != t || last_compare_set != last_compare); |
| 113 | |
| 114 | /* convert ticks to msec */ |
| 115 | delta = (delta - last_compare - tick_interval) / (tick_rate_mhz * 1000); |
| 116 | lk_time_t res = (t * tick_interval_ms) + delta; |
| 117 | |
| 118 | return res; |
| 119 | } |
| 120 | |
| 121 | lk_bigtime_t current_time_hires(void) |
| 122 | { |
| 123 | uint64_t t; |
| 124 | uint32_t last_compare; |
| 125 | uint32_t delta; |
| 126 | |
| 127 | /* sample the tick counter, the last compare register set, and the current count atomically */ |
| 128 | do { |
| 129 | t = ticks; |
| 130 | last_compare = last_compare_set; |
| 131 | delta = mips_read_c0_count(); |
| 132 | } while (ticks != t); |
| 133 | |
| 134 | /* convert ticks to usec */ |
| 135 | delta = (delta - last_compare - tick_interval) / tick_rate_mhz; |
| 136 | lk_bigtime_t res = (t * tick_interval_us) + delta; |
| 137 | |
| 138 | return res; |
| 139 | } |
| 140 | |
| 141 | void mips_init_timer(uint32_t freq) |
| 142 | { |
| 143 | tick_rate = freq; |
| 144 | tick_rate_mhz = freq / 1000000; |
| 145 | |
| 146 | // disable the counter |
| 147 | mips_write_c0_cause(mips_read_c0_cause() | (1<<27)); |
| 148 | |
| 149 | // figure out which interrupt the timer is set to |
| 150 | uint32_t ipti = BITS_SHIFT(mips_read_c0_intctl(), 31, 29); |
| 151 | if (ipti >= 2) { |
| 152 | mips_enable_irq(ipti); |
| 153 | } |
| 154 | } |
| 155 | |