Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 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 <sys/types.h> |
| 24 | #include <err.h> |
| 25 | #include <kernel/thread.h> |
| 26 | #include <platform.h> |
| 27 | #include <platform/interrupts.h> |
| 28 | #include <platform/timer.h> |
| 29 | #include <platform/armemu.h> |
| 30 | #include "platform_p.h" |
| 31 | |
| 32 | static platform_timer_callback t_callback; |
| 33 | |
| 34 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval) |
| 35 | { |
| 36 | enter_critical_section(); |
| 37 | |
| 38 | t_callback = callback; |
| 39 | |
| 40 | *REG(PIT_CLEAR) = 1; |
| 41 | *REG(PIT_INTERVAL) = interval; |
| 42 | *REG(PIT_START_PERIODIC) = 1; |
| 43 | |
Travis Geiselbrecht | f1cdea3 | 2008-09-13 14:13:54 -0700 | [diff] [blame] | 44 | unmask_interrupt(INT_PIT); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 45 | |
| 46 | exit_critical_section(); |
| 47 | |
| 48 | return NO_ERROR; |
| 49 | } |
| 50 | |
Travis Geiselbrecht | e74d63d | 2010-05-06 13:35:49 -0700 | [diff] [blame] | 51 | bigtime_t current_time_hires(void) |
| 52 | { |
| 53 | bigtime_t time; |
| 54 | *REG(SYSINFO_TIME_LATCH) = 1; |
| 55 | time = *REG(SYSINFO_TIME_SECS) * 1000000ULL; |
| 56 | time += *REG(SYSINFO_TIME_USECS); |
| 57 | |
| 58 | return time; |
| 59 | } |
| 60 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 61 | time_t current_time(void) |
| 62 | { |
| 63 | time_t time; |
| 64 | *REG(SYSINFO_TIME_LATCH) = 1; |
| 65 | time = *REG(SYSINFO_TIME_SECS) * 1000; |
| 66 | time += *REG(SYSINFO_TIME_USECS) / 1000; |
Travis Geiselbrecht | e74d63d | 2010-05-06 13:35:49 -0700 | [diff] [blame] | 67 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 68 | return time; |
| 69 | } |
| 70 | |
| 71 | static enum handler_return platform_tick(void *arg) |
| 72 | { |
| 73 | *REG(PIT_CLEAR_INT) = 1; |
| 74 | if (t_callback) { |
| 75 | return t_callback(arg, current_time()); |
| 76 | } else { |
| 77 | return INT_NO_RESCHEDULE; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void platform_init_timer(void) |
| 82 | { |
| 83 | register_int_handler(INT_PIT, &platform_tick, NULL); |
| 84 | } |
| 85 | |