Corey Tabaka | 8469724 | 2009-03-26 02:32:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Corey Tabaka |
| 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 <sys/types.h> |
| 24 | #include <err.h> |
| 25 | #include <reg.h> |
| 26 | #include <debug.h> |
| 27 | #include <kernel/thread.h> |
| 28 | #include <platform.h> |
| 29 | #include <platform/interrupts.h> |
| 30 | #include <platform/console.h> |
| 31 | #include <platform/timer.h> |
Corey Tabaka | b3f6cac | 2009-04-01 17:35:12 -0400 | [diff] [blame] | 32 | #include <platform/pc.h> |
Corey Tabaka | 8469724 | 2009-03-26 02:32:01 -0400 | [diff] [blame] | 33 | #include "platform_p.h" |
| 34 | #include <arch/x86.h> |
| 35 | |
| 36 | static platform_timer_callback t_callback; |
| 37 | static void *callback_arg; |
| 38 | |
| 39 | static uint64_t next_trigger_time; |
| 40 | static uint64_t next_trigger_delta; |
| 41 | |
| 42 | static uint64_t timer_delta_time; |
| 43 | static uint64_t timer_current_time; |
| 44 | |
| 45 | static uint16_t divisor; |
| 46 | |
| 47 | #define INTERNAL_FREQ 1193182ULL |
| 48 | #define INTERNAL_FREQ_3X 3579546ULL |
| 49 | |
| 50 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval) |
| 51 | { |
| 52 | enter_critical_section(); |
| 53 | |
| 54 | t_callback = callback; |
| 55 | callback_arg = arg; |
| 56 | |
| 57 | next_trigger_delta = (uint64_t) interval << 32; |
| 58 | next_trigger_time = timer_current_time + next_trigger_delta; |
| 59 | |
| 60 | exit_critical_section(); |
| 61 | |
| 62 | return NO_ERROR; |
| 63 | } |
| 64 | |
| 65 | time_t current_time(void) |
| 66 | { |
| 67 | time_t time; |
| 68 | |
| 69 | enter_critical_section(); |
| 70 | time = (time_t) (timer_current_time >> 32); |
| 71 | exit_critical_section(); |
| 72 | |
| 73 | return time; |
| 74 | } |
| 75 | |
| 76 | bigtime_t current_time_hires(void) |
| 77 | { |
| 78 | bigtime_t time; |
| 79 | |
| 80 | enter_critical_section(); |
| 81 | time = (bigtime_t) ((timer_current_time >> 22) * 1000) >> 10; |
| 82 | exit_critical_section(); |
| 83 | |
| 84 | return time; |
| 85 | } |
| 86 | static enum handler_return os_timer_tick(void *arg) |
| 87 | { |
| 88 | uint64_t delta; |
| 89 | |
| 90 | timer_current_time += timer_delta_time; |
| 91 | |
| 92 | time_t time = current_time(); |
| 93 | //bigtime_t btime = current_time_hires(); |
| 94 | //printf_xy(71, 0, WHITE, "%08u", (uint32_t) time); |
| 95 | //printf_xy(63, 1, WHITE, "%016llu", (uint64_t) btime); |
| 96 | |
| 97 | if (t_callback && timer_current_time >= next_trigger_time) { |
| 98 | delta = timer_current_time - next_trigger_time; |
| 99 | next_trigger_time = timer_current_time + next_trigger_delta - delta; |
| 100 | |
| 101 | return t_callback(callback_arg, time); |
| 102 | } else { |
| 103 | return INT_NO_RESCHEDULE; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void set_pit_frequency(uint32_t frequency) |
| 108 | { |
| 109 | uint32_t count, remainder; |
| 110 | |
| 111 | /* figure out the correct divisor for the desired frequency */ |
| 112 | if (frequency <= 18) { |
| 113 | count = 0xffff; |
| 114 | } else if (frequency >= INTERNAL_FREQ) { |
| 115 | count = 1; |
| 116 | } else { |
| 117 | count = INTERNAL_FREQ_3X / frequency; |
| 118 | remainder = INTERNAL_FREQ_3X % frequency; |
| 119 | |
| 120 | if (remainder >= INTERNAL_FREQ_3X / 2) { |
| 121 | count += 1; |
| 122 | } |
| 123 | |
| 124 | count /= 3; |
| 125 | remainder = count % 3; |
| 126 | |
| 127 | if (remainder >= 1) { |
| 128 | count += 1; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | divisor = count & 0xffff; |
| 133 | |
| 134 | /* |
| 135 | * funky math that i don't feel like explaining. essentially 32.32 fixed |
| 136 | * point representation of the configured timer delta. |
| 137 | */ |
| 138 | timer_delta_time = (3685982306ULL * count) >> 10; |
| 139 | |
| 140 | //dprintf(DEBUG, "set_pit_frequency: dt=%016llx\n", timer_delta_time); |
| 141 | //dprintf(DEBUG, "set_pit_frequency: divisor=%04x\n", divisor); |
| 142 | |
| 143 | /* |
| 144 | * setup the Programmable Interval Timer |
| 145 | * timer 0, mode 2, binary counter, LSB followed by MSB |
| 146 | */ |
| 147 | outp(I8253_CONTROL_REG, 0x34); |
| 148 | outp(I8253_DATA_REG, divisor & 0xff); // LSB |
| 149 | outp(I8253_DATA_REG, divisor >> 8); // MSB |
| 150 | } |
| 151 | |
| 152 | void platform_init_timer(void) |
| 153 | { |
| 154 | timer_current_time = 0; |
| 155 | |
| 156 | set_pit_frequency(1000); // ~1ms granularity |
| 157 | |
| 158 | register_int_handler(INT_PIT, &os_timer_tick, NULL); |
| 159 | unmask_interrupt(INT_PIT); |
| 160 | } |
| 161 | |
| 162 | void platform_halt_timers(void) |
| 163 | { |
| 164 | mask_interrupt(INT_PIT); |
| 165 | } |
| 166 | |