1 /* 2 * Copyright (c) 2023 Intel Corporation 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <sedi_driver_rtc.h> 8 9 /* driver version */ 10 #define SEDI_RTC_DRIVER_VERSION SEDI_DRIVER_VERSION_MAJOR_MINOR(0, 1) 11 12 #ifndef SEDI_RTC_TICKS_PER_SECOND 13 #define SEDI_RTC_TICKS_PER_SECOND 32768 14 #endif 15 16 #ifndef USECS_PER_SEC 17 #define USECS_PER_SEC 1000000 18 #endif 19 20 /* driver version */ 21 static const sedi_driver_version_t driver_version = { SEDI_RTC_API_VERSION, 22 SEDI_RTC_DRIVER_VERSION }; 23 24 /* driver capabilities */ 25 static const sedi_rtc_capabilities_t driver_capabilities = { .support_alarm = 0, 26 .support_callback = 0, 27 .reserved = 0 }; 28 sedi_rtc_to_us(uint64_t rtc_ticks)29static inline uint64_t sedi_rtc_to_us(uint64_t rtc_ticks) 30 { 31 return (rtc_ticks * USECS_PER_SEC) / SEDI_RTC_TICKS_PER_SECOND; 32 } 33 sedi_rtc_get_version(void)34sedi_driver_version_t sedi_rtc_get_version(void) 35 { 36 return driver_version; 37 } 38 sedi_rtc_get_capabilities(sedi_rtc_capabilities_t * cap)39int sedi_rtc_get_capabilities(sedi_rtc_capabilities_t *cap) 40 { 41 SEDI_ASSERT(cap); 42 43 cap->support_alarm = driver_capabilities.support_alarm; 44 cap->support_callback = driver_capabilities.support_callback; 45 46 return SEDI_DRIVER_OK; 47 } 48 sedi_rtc_init(void)49int sedi_rtc_init(void) 50 { 51 return SEDI_DRIVER_OK; 52 } 53 sedi_rtc_uninit(void)54int sedi_rtc_uninit(void) 55 { 56 return SEDI_DRIVER_OK; 57 } 58 sedi_rtc_set_power(sedi_power_state_t state)59int sedi_rtc_set_power(sedi_power_state_t state) 60 { 61 PARAM_UNUSED(state); 62 63 return SEDI_DRIVER_OK; 64 } 65 sedi_rtc_get(void)66uint64_t sedi_rtc_get(void) 67 { 68 uint32_t lower; 69 uint32_t upper; 70 71 do { 72 upper = read32(SEDI_RTC_COUNTER1); 73 lower = read32(SEDI_RTC_COUNTER0); 74 } while (upper != read32(SEDI_RTC_COUNTER1)); 75 76 return ((uint64_t)upper << 32U) | lower; 77 } 78 sedi_rtc_get_us(void)79uint64_t sedi_rtc_get_us(void) 80 { 81 uint64_t rtc = sedi_rtc_get(); 82 83 return sedi_rtc_to_us(rtc); 84 } 85