-
Notifications
You must be signed in to change notification settings - Fork 118
Implement Goldfish RTC #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChinYikMing
wants to merge
6
commits into
sysprog21:master
Choose a base branch
from
ChinYikMing:rtc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9452d47
Implement Goldfish RTC
ChinYikMing cb8e07e
Enable Goldfish RTC driver in linux config
ChinYikMing 1b91d35
Enable hwclock utility in buildroot config
ChinYikMing 0ae1bb2
Enable package patching for Buildroot
ChinYikMing 4e7ed84
Add userspace programs for /dev/rtc?
ChinYikMing 5ee534a
CI: Add RTC test coverage
ChinYikMing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* rv32emu is freely redistributable under the MIT License. See the file | ||
* "LICENSE" for information on usage and redistribution of this file. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
|
||
#include "rtc.h" | ||
|
||
static uint64_t now_nsec; | ||
|
||
uint64_t get_now_nsec(rtc_t *rtc) | ||
{ | ||
/* TODO: | ||
* - detects timezone and use the correct UTC offset | ||
* - a new CLI option should be added to main.c to let user to select | ||
* [UTC] or [UTC + offset](localtime) time. E.g., -x rtc:utc or -x | ||
* rtc:localtime | ||
*/ | ||
struct timespec ts; | ||
clock_gettime(CLOCK_REALTIME, &ts); | ||
return (uint64_t) (ts.tv_sec * 1e9) + ts.tv_nsec + rtc->clock_offset; | ||
} | ||
|
||
uint32_t rtc_read(rtc_t *rtc, uint32_t addr) | ||
{ | ||
uint32_t rtc_read_val = 0; | ||
|
||
switch (addr) { | ||
case RTC_TIME_LOW: | ||
now_nsec = get_now_nsec(rtc); | ||
rtc->time_low = (uint32_t) (now_nsec & MASK(32)); | ||
rtc_read_val = rtc->time_low; | ||
break; | ||
case RTC_TIME_HIGH: | ||
/* reuse the now_nsec when reading RTC_TIME_LOW */ | ||
rtc->time_high = (uint32_t) (now_nsec >> 32); | ||
rtc_read_val = rtc->time_high; | ||
break; | ||
case RTC_ALARM_LOW: | ||
rtc_read_val = rtc->alarm_low; | ||
break; | ||
case RTC_ALARM_HIGH: | ||
rtc_read_val = rtc->alarm_high; | ||
break; | ||
case RTC_ALARM_STATUS: | ||
rtc_read_val = rtc->alarm_status; | ||
break; | ||
default: | ||
rv_log_error("Unsupported RTC read operation, 0x%x", addr); | ||
break; | ||
} | ||
|
||
return rtc_read_val; | ||
} | ||
|
||
void rtc_write(rtc_t *rtc, uint32_t addr, uint32_t value) | ||
{ | ||
switch (addr) { | ||
case RTC_TIME_LOW: | ||
now_nsec = get_now_nsec(rtc); | ||
rtc->clock_offset += (uint64_t) (value) - (now_nsec & MASK(32)); | ||
break; | ||
case RTC_TIME_HIGH: | ||
/* reuse the now_nsec when writing RTC_TIME_LOW */ | ||
rtc->clock_offset += ((uint64_t) (value) << 32) - | ||
(now_nsec & ((uint64_t) (MASK(32)) << 32)); | ||
break; | ||
case RTC_ALARM_LOW: | ||
rtc->alarm_low = value; | ||
break; | ||
case RTC_ALARM_HIGH: | ||
rtc->alarm_high = value; | ||
break; | ||
case RTC_IRQ_ENABLED: | ||
rtc->irq_enabled = value; | ||
break; | ||
case RTC_CLEAR_ALARM: | ||
rtc->clear_alarm = value; | ||
break; | ||
case RTC_CLEAR_INTERRUPT: | ||
rtc->clear_interrupt = value; | ||
rtc->interrupt_status = 0; | ||
break; | ||
default: | ||
rv_log_error("Unsupported RTC write operation, 0x%x", addr); | ||
break; | ||
} | ||
return; | ||
} | ||
|
||
rtc_t *rtc_new() | ||
{ | ||
rtc_t *rtc = calloc(1, sizeof(rtc_t)); | ||
assert(rtc); | ||
|
||
/* | ||
* The rtc->time_low/high values can be updated through the RTC_SET_TIME | ||
* ioctl operation. Therefore, they should be initialized to match the | ||
* host OS time during initialization. | ||
*/ | ||
now_nsec = get_now_nsec(rtc); | ||
rtc->time_low = (uint32_t) (now_nsec & MASK(32)); | ||
rtc->time_high = (uint32_t) (now_nsec >> 32); | ||
|
||
return rtc; | ||
} | ||
|
||
void rtc_delete(rtc_t *rtc) | ||
{ | ||
free(rtc); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.