|
| 1 | +/* |
| 2 | + * rv32emu is freely redistributable under the MIT License. See the file |
| 3 | + * "LICENSE" for information on usage and redistribution of this file. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <assert.h> |
| 7 | +#include <errno.h> |
| 8 | +#include <fcntl.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string.h> |
| 13 | +#include <time.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +#include "rtc.h" |
| 17 | + |
| 18 | +static uint64_t now_nsec; |
| 19 | + |
| 20 | +uint64_t get_now_nsec(rtc_t *rtc) |
| 21 | +{ |
| 22 | + /* TODO: |
| 23 | + * - detects timezone and use the correct UTC offset |
| 24 | + * - a new CLI option should be added to main.c to let user to select |
| 25 | + * [UTC] or [UTC + offset](localtime) time. E.g., -x rtc:utc or -x |
| 26 | + * rtc:localtime |
| 27 | + */ |
| 28 | + struct timespec ts; |
| 29 | + clock_gettime(CLOCK_REALTIME, &ts); |
| 30 | + return (uint64_t) (ts.tv_sec * 1e9) + ts.tv_nsec + rtc->clock_offset; |
| 31 | +} |
| 32 | + |
| 33 | +uint32_t rtc_read(rtc_t *rtc, uint32_t addr) |
| 34 | +{ |
| 35 | + uint32_t rtc_read_val = 0; |
| 36 | + |
| 37 | + switch (addr) { |
| 38 | + case RTC_TIME_LOW: |
| 39 | + now_nsec = get_now_nsec(rtc); |
| 40 | + rtc->time_low = (uint32_t) (now_nsec & MASK(32)); |
| 41 | + rtc_read_val = rtc->time_low; |
| 42 | + break; |
| 43 | + case RTC_TIME_HIGH: |
| 44 | + /* reuse the now_nsec when reading RTC_TIME_LOW */ |
| 45 | + rtc->time_high = (uint32_t) (now_nsec >> 32); |
| 46 | + rtc_read_val = rtc->time_high; |
| 47 | + break; |
| 48 | + case RTC_ALARM_LOW: |
| 49 | + rtc_read_val = rtc->alarm_low; |
| 50 | + break; |
| 51 | + case RTC_ALARM_HIGH: |
| 52 | + rtc_read_val = rtc->alarm_high; |
| 53 | + break; |
| 54 | + case RTC_ALARM_STATUS: |
| 55 | + rtc_read_val = rtc->alarm_status; |
| 56 | + break; |
| 57 | + default: |
| 58 | + rv_log_error("Unsupported RTC read operation, 0x%x", addr); |
| 59 | + break; |
| 60 | + } |
| 61 | + |
| 62 | + return rtc_read_val; |
| 63 | +} |
| 64 | + |
| 65 | +void rtc_write(rtc_t *rtc, uint32_t addr, uint32_t value) |
| 66 | +{ |
| 67 | + switch (addr) { |
| 68 | + case RTC_TIME_LOW: |
| 69 | + now_nsec = get_now_nsec(rtc); |
| 70 | + rtc->clock_offset += (uint64_t) (value) - (now_nsec & MASK(32)); |
| 71 | + break; |
| 72 | + case RTC_TIME_HIGH: |
| 73 | + /* reuse the now_nsec when writing RTC_TIME_LOW */ |
| 74 | + rtc->clock_offset += ((uint64_t) (value) << 32) - |
| 75 | + (now_nsec & ((uint64_t) (MASK(32)) << 32)); |
| 76 | + break; |
| 77 | + case RTC_ALARM_LOW: |
| 78 | + rtc->alarm_low = value; |
| 79 | + break; |
| 80 | + case RTC_ALARM_HIGH: |
| 81 | + rtc->alarm_high = value; |
| 82 | + break; |
| 83 | + case RTC_IRQ_ENABLED: |
| 84 | + rtc->irq_enabled = value; |
| 85 | + break; |
| 86 | + case RTC_CLEAR_ALARM: |
| 87 | + rtc->clear_alarm = value; |
| 88 | + break; |
| 89 | + case RTC_CLEAR_INTERRUPT: |
| 90 | + rtc->clear_interrupt = value; |
| 91 | + rtc->interrupt_status = 0; |
| 92 | + break; |
| 93 | + default: |
| 94 | + rv_log_error("Unsupported RTC write operation, 0x%x", addr); |
| 95 | + break; |
| 96 | + } |
| 97 | + return; |
| 98 | +} |
| 99 | + |
| 100 | +rtc_t *rtc_new() |
| 101 | +{ |
| 102 | + rtc_t *rtc = calloc(1, sizeof(rtc_t)); |
| 103 | + assert(rtc); |
| 104 | + |
| 105 | + /* |
| 106 | + * The rtc->time_low/high values can be updated through the RTC_SET_TIME |
| 107 | + * ioctl operation. Therefore, they should be initialized to match the |
| 108 | + * host OS time during initialization. |
| 109 | + */ |
| 110 | + now_nsec = get_now_nsec(rtc); |
| 111 | + rtc->time_low = (uint32_t) (now_nsec & MASK(32)); |
| 112 | + rtc->time_high = (uint32_t) (now_nsec >> 32); |
| 113 | + |
| 114 | + return rtc; |
| 115 | +} |
| 116 | + |
| 117 | +void rtc_delete(rtc_t *rtc) |
| 118 | +{ |
| 119 | + free(rtc); |
| 120 | +} |
0 commit comments