rtc_acts.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Copyright (c) 2018 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief RTC driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <device.h>
  12. #include <init.h>
  13. #include <kernel.h>
  14. #include <string.h>
  15. #include <soc.h>
  16. #include <board_cfg.h>
  17. #include <drivers/rtc.h>
  18. #include <logging/log.h>
  19. //#include <ksched.h>
  20. #if (CONFIG_PM_BACKUP_TIME_FUNCTION_EN == 1)
  21. #include <drivers/nvram_config.h>
  22. #endif
  23. LOG_MODULE_REGISTER(rtc0, CONFIG_LOG_DEFAULT_LEVEL);
  24. #define RTC_DEFAULT_INIT_TIME (1483200000) /* 2016.12.31 16:00:00 */
  25. /* RTC Control Register */
  26. #define RTC_CTL_ALIP BIT(0)
  27. #define RTC_CTL_ALIE BIT(1)
  28. #define RTC_CTL_4HZ_CLK_SEL_SHIFT (2)
  29. #define RTC_CTL_4HZ_CLK_SEL_MASK (3 << RTC_CTL_4HZ_CLK_SEL_SHIFT)
  30. #define RTC_CTL_4HZ_CLK_SEL(x) ((x) << RTC_CTL_4HZ_CLK_SEL_SHIFT)
  31. #define RTC_CTL_4HZ_CLK_HCL RTC_CTL_4HZ_CLK_SEL(0)
  32. #define RTC_CTL_4HZ_CLK_32KHZ RTC_CTL_4HZ_CLK_SEL(1)
  33. #define RTC_CTL_4HZ_CLK_HOSC RTC_CTL_4HZ_CLK_SEL(2)
  34. #define RTC_CTL_CALEN BIT(4)
  35. #define RTC_CTL_LEAP BIT(7)
  36. #define RTC_CTL_32KHZ_SEL BIT(16) /* 0: LOSC 32KHZ; 1 EXT_32KHZ */
  37. #define RTC_CTL_CAL_CLK_SEL BIT(17) /* 0: 4HZ; 1: 100HZ */
  38. #define RTC_CTL_TESTEN BIT(18)
  39. #define RTC_MS_SHIRT (0) /* millisecode x 10 */
  40. #define RTC_MS_MASK (0x7f << RTC_MS_SHIRT)
  41. #define RTC_MS(x) (((x) & RTC_MS_MASK) >> RTC_MS_SHIRT)
  42. #define RTC_MS_MUL10(x) (RTC_MS(x) * 10)
  43. #define RTC_MS_DIV10(x) (((x) / 10) << RTC_MS_SHIRT)
  44. #define RTC_YMD_Y_SHIFT (16)
  45. #define RTC_YMD_Y_MASK (0x7f << RTC_YMD_Y_SHIFT)
  46. #define RTC_YMD_M_SHIFT (8)
  47. #define RTC_YMD_M_MASK (0xf << RTC_YMD_M_SHIFT)
  48. #define RTC_YMD_D_SHIFT (0)
  49. #define RTC_YMD_D_MASK (0x1f << RTC_YMD_D_SHIFT)
  50. #define RTC_YMD_Y(ymd) (((ymd) & RTC_YMD_Y_MASK) >> RTC_YMD_Y_SHIFT)
  51. #define RTC_YMD_M(ymd) (((ymd) & RTC_YMD_M_MASK) >> RTC_YMD_M_SHIFT)
  52. #define RTC_YMD_D(ymd) (((ymd) & RTC_YMD_D_MASK) >> RTC_YMD_D_SHIFT)
  53. #define RTC_YMD_VAL(y, m, d) (((y) << RTC_YMD_Y_SHIFT) | ((m) << RTC_YMD_M_SHIFT) | ((d) << RTC_YMD_D_SHIFT))
  54. #define RTC_HMS_H_SHIFT (16)
  55. #define RTC_HMS_H_MASK (0x1f << RTC_HMS_H_SHIFT)
  56. #define RTC_HMS_M_SHIFT (8)
  57. #define RTC_HMS_M_MASK (0x3f << RTC_HMS_M_SHIFT)
  58. #define RTC_HMS_S_SHIFT (0)
  59. #define RTC_HMS_S_MASK (0x3f << RTC_HMS_S_SHIFT)
  60. #define RTC_HMS_H(ymd) (((ymd) & RTC_HMS_H_MASK) >> RTC_HMS_H_SHIFT)
  61. #define RTC_HMS_M(ymd) (((ymd) & RTC_HMS_M_MASK) >> RTC_HMS_M_SHIFT)
  62. #define RTC_HMS_S(ymd) (((ymd) & RTC_HMS_S_MASK) >> RTC_HMS_S_SHIFT)
  63. #define RTC_HMS_VAL(h, m, s) (((h) << RTC_HMS_H_SHIFT) | ((m) << RTC_HMS_M_SHIFT) | ((s) << RTC_HMS_S_SHIFT))
  64. #define RTC_REGS_UPDATE_MAGIC (0xA596)
  65. #define RTC_REGS_UPDATE_OK (0x5A69)
  66. #define RTC_CONFIG_PERIOD_ALARM_TIMEOUT_MS (1000)
  67. enum acts_rtc_clock_src {
  68. RTC_CLKSRC_HOSC_4HZ = 0,
  69. RTC_CLKSRC_HCL_LOSC_100HZ,
  70. RTC_CLKSRC_HCL_100HZ,
  71. };
  72. struct acts_rtc_controller {
  73. volatile uint32_t ctrl;
  74. volatile uint32_t regupdata;
  75. volatile uint32_t ms_alarm;
  76. volatile uint32_t hms_alarm;
  77. volatile uint32_t ymd_alarm;
  78. volatile uint32_t ms; /* millisecond x 10 */
  79. volatile uint32_t hms;
  80. volatile uint32_t ymd;
  81. };
  82. struct acts_rtc_data {
  83. struct acts_rtc_controller *base;
  84. void (*alarm_cb_fn)(const void *cb_data);
  85. const void *cb_data;
  86. struct rtc_alarm_period_config period_config;
  87. bool alarm_en;
  88. uint8_t alarm_wakeup : 1; /* System wakeup from RTC alarm indicator */
  89. uint8_t is_need_sync : 1; /* If 1 to sync ahb clock */
  90. uint8_t is_set_sync : 1; /* Set the RTC time to ensure that it waits for 3 rtc clk(30ms) before entering sleep mode */
  91. uint32_t syc_st_cycle;
  92. uint32_t set_st_ms;
  93. };
  94. struct acts_rtc_config {
  95. struct acts_rtc_controller *base;
  96. uint8_t rtc_clk_source;
  97. void (*irq_config)(void);
  98. };
  99. static int rtc_acts_set_alarm(const struct device *dev, struct rtc_alarm_config *config, bool enable);
  100. static int __rtc_acts_set_alarm_period(const struct device *dev);
  101. static void rtc_acts_dump_regs(struct acts_rtc_controller *base)
  102. {
  103. LOG_INF("** RTC Controller register **");
  104. LOG_INF(" BASE: 0x%x", (uint32_t)base);
  105. LOG_INF(" CTL: 0x%x", base->ctrl);
  106. LOG_INF(" REGUPDATE: 0x%x", base->regupdata);
  107. LOG_INF(" MSALM: 0x%x", base->ms_alarm);
  108. LOG_INF(" DHMSALM: 0x%x", base->hms_alarm);
  109. LOG_INF(" YMDALM: 0x%x", base->ymd_alarm);
  110. LOG_INF(" MS: 0x%x", base->ms);
  111. LOG_INF(" DHMS: 0x%x", base->hms);
  112. LOG_INF(" YMD: 0x%x", base->ymd);
  113. }
  114. /* RTC associated registers update to take effect */
  115. void rtc_acts_update_regs(void)
  116. {
  117. #if !defined(CONFIG_SOC_SERIES_LARK) && !defined(CONFIG_SOC_SERIES_LEOPARD)
  118. struct acts_rtc_controller *base = (struct acts_rtc_controller *)RTC_REG_BASE;
  119. base->regupdata = RTC_REGS_UPDATE_MAGIC;
  120. while (base->regupdata != RTC_REGS_UPDATE_OK) {
  121. ; // wait rtc register update
  122. }
  123. #endif
  124. }
  125. /* Select the LOSC as the clock source which is inside of the HCL controller. */
  126. static inline void rtc_acts_init_clksource(const struct device *dev, struct acts_rtc_data *rtcd)
  127. {
  128. struct acts_rtc_controller *base = rtcd->base;
  129. const struct acts_rtc_config *cfg = dev->config;
  130. if (cfg->rtc_clk_source == RTC_CLKSRC_HOSC_4HZ) {
  131. base->ctrl &= ~RTC_CTL_4HZ_CLK_SEL_MASK;
  132. base->ctrl |= RTC_CTL_4HZ_CLK_HOSC;
  133. } else if (cfg->rtc_clk_source == RTC_CLKSRC_HCL_100HZ){
  134. base->ctrl = RTC_CTL_ALIP | RTC_CTL_4HZ_CLK_HCL; // sel hcl
  135. sys_write32(0, HCL_CTL);
  136. k_busy_wait(500);
  137. //sys_write32(0x8c1, HCL_CTL);// period 4s cal:1s
  138. sys_write32(0xcc1, HCL_CTL);// period 8s cal:1s
  139. base->ctrl |= RTC_CTL_CAL_CLK_SEL; /* select 100HZ */
  140. }else { // RTC_CLKSRC_HCL_LOSC_100HZ
  141. /* WIO2 => LOSCI; WIO3 => LOSCO */
  142. sys_write32(1 << 4, WIO2_CTL);
  143. sys_write32(1 << 4, WIO3_CTL);
  144. /* enable LOSC */
  145. sys_write32(0x1a1a71, LOSC_CTL);
  146. base->ctrl = RTC_CTL_ALIP | RTC_CTL_4HZ_CLK_32KHZ; // sel losc
  147. sys_write32(0, HCL_CTL);
  148. k_busy_wait(500);
  149. sys_write32(0x0c8c, HCL_CTL);
  150. base->ctrl |= RTC_CTL_CAL_CLK_SEL; /* select 100HZ */
  151. }
  152. rtc_acts_update_regs();
  153. LOG_DBG("rtc_ctl:0x%08x", base->ctrl);
  154. }
  155. /**
  156. * HW ISSUE:
  157. * software need sleep over 1 RTC period to sync AHB clock.
  158. */
  159. static int rtc_pm_sync_ahb(struct acts_rtc_data *rtcd)
  160. {
  161. struct acts_rtc_controller *base = rtcd->base;
  162. uint32_t ymd, hms, ms;
  163. uint32_t sysn_time_ms;
  164. if (rtcd->is_need_sync)
  165. rtcd->is_need_sync = 0;
  166. else
  167. return 0;
  168. ymd = base->ymd;
  169. hms = base->hms;
  170. ms = base->ms;
  171. ms = k_cyc_to_ms_near32(k_cycle_get_32()-rtcd->syc_st_cycle);
  172. #if (CONFIG_RTC_CLK_SOURCE == 0)
  173. if(ms >= 300)
  174. sysn_time_ms = 0;
  175. else
  176. sysn_time_ms = 300 - ms; // need over 1 rtc clk
  177. #else
  178. if(ms >= 11)
  179. sysn_time_ms = 0;
  180. else
  181. sysn_time_ms = 11 - ms; // need over 1 rtc clk
  182. #endif
  183. if(sysn_time_ms){
  184. if (k_is_in_isr())
  185. k_busy_wait(sysn_time_ms * 1000);
  186. else
  187. k_sleep(K_MSEC(sysn_time_ms));
  188. }
  189. ymd = base->ymd;
  190. hms = base->hms;
  191. ms = base->ms;
  192. return 0;
  193. }
  194. static int64_t rtc_base_ms, rtc_sys_ms = 0;
  195. static struct acts_rtc_controller *rtc_reg = NULL;
  196. static int64_t rtc_acts_get_ms(void)
  197. {
  198. int64_t all_ms;
  199. uint32_t ymd, hms, time;
  200. uint8_t ms;
  201. struct rtc_time tm;
  202. if(rtc_reg == NULL)
  203. return 0;
  204. ymd = rtc_reg->ymd;
  205. hms = rtc_reg->hms;
  206. ms = rtc_reg->ms;
  207. tm.tm_ms = RTC_MS_MUL10(ms); /* rtc milisecond 0 ~ 999 */
  208. tm.tm_sec = RTC_HMS_S(hms); /* rtc second 0-59 */
  209. tm.tm_min = RTC_HMS_M(hms); /* rtc minute 0-59 */
  210. tm.tm_hour = RTC_HMS_H(hms); /* rtc hour 0-23 */
  211. tm.tm_mday = RTC_YMD_D(ymd); /* rtc day 1-31 */
  212. tm.tm_mon = RTC_YMD_M(ymd) - 1; /* rtc mon 1-12 */
  213. tm.tm_year = 100 + RTC_YMD_Y(ymd); /* rtc year 0-99 */
  214. rtc_tm_to_time(&tm, &time);
  215. all_ms = time;
  216. all_ms = all_ms*1000+ms;
  217. return all_ms;
  218. }
  219. static void rtc_acts_update_base(struct rtc_time *tm)
  220. {
  221. uint32_t time;
  222. int64_t cur_ms = rtc_acts_get_ms();
  223. rtc_sys_ms = rtc_sys_ms + (cur_ms - rtc_base_ms) +150;//150 lost
  224. rtc_tm_to_time(tm, &time);
  225. rtc_base_ms = time;
  226. rtc_base_ms = rtc_base_ms*1000+tm->tm_ms;
  227. }
  228. static void rtc_acts_base_init(void)
  229. {
  230. rtc_base_ms = rtc_acts_get_ms();
  231. rtc_sys_ms = rtc_base_ms;
  232. }
  233. int64_t k_uptime_get_by_rtc(void)
  234. {
  235. uint32_t key;
  236. int64_t cur_ms;
  237. key = irq_lock();
  238. cur_ms = rtc_sys_ms + (rtc_acts_get_ms() - rtc_base_ms);
  239. irq_unlock(key);
  240. return cur_ms;
  241. }
  242. static int rtc_acts_get_datetime(struct acts_rtc_data *rtcd, struct rtc_time *tm)
  243. {
  244. struct acts_rtc_controller *base = rtcd->base;
  245. uint32_t ymd, hms, time;
  246. uint8_t ms;
  247. rtc_pm_sync_ahb(rtcd);
  248. ymd = base->ymd;
  249. hms = base->hms;
  250. ms = base->ms;
  251. tm->tm_ms = RTC_MS_MUL10(ms); /* rtc milisecond 0 ~ 999 */
  252. tm->tm_sec = RTC_HMS_S(hms); /* rtc second 0-59 */
  253. tm->tm_min = RTC_HMS_M(hms); /* rtc minute 0-59 */
  254. tm->tm_hour = RTC_HMS_H(hms); /* rtc hour 0-23 */
  255. tm->tm_mday = RTC_YMD_D(ymd); /* rtc day 1-31 */
  256. tm->tm_wday = 0;
  257. tm->tm_mon = RTC_YMD_M(ymd) - 1; /* rtc mon 1-12 */
  258. tm->tm_year = 100 + RTC_YMD_Y(ymd); /* rtc year 0-99 */
  259. LOG_DBG("read date time: %04d-%02d-%02d %02d:%02d:%02d:%03d",
  260. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  261. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_ms);
  262. rtc_tm_to_time(tm, &time);
  263. /* day of the week, 1970-01-01 was a Thursday */
  264. tm->tm_wday = (time / 86400 + 4) % 7;
  265. /* the clock can give out invalid datetime, but we cannot return
  266. * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  267. */
  268. if (rtc_valid_tm(tm) < 0) {
  269. LOG_ERR("rtc: retrieved date/time is not valid.\n");
  270. rtc_acts_dump_regs(base);
  271. }
  272. return 0;
  273. }
  274. static int rtc_acts_set_datetime(struct acts_rtc_data *rtcd, struct rtc_time *tm)
  275. {
  276. struct acts_rtc_controller *base = rtcd->base;
  277. uint32_t key;
  278. LOG_INF("set datetime: %04d-%02d-%02d %02d:%02d:%02d:%03d",
  279. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  280. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_ms);
  281. /* disable calendar when update the hms register
  282. * From the specification discription, the CAL_EN bit must be disable when
  283. * the RTC_DHMS/RTC_YMD registers being written. And RTC_DHMS/RTC_YMD
  284. * registers must be written before CAL_EN is enabled.
  285. */
  286. key = irq_lock();
  287. rtc_acts_update_base(tm);
  288. base->ctrl &= ~RTC_CTL_CALEN;
  289. rtc_acts_update_regs();
  290. base->ymd = RTC_YMD_VAL(tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday);
  291. base->hms = RTC_HMS_VAL(tm->tm_hour, tm->tm_min, tm->tm_sec);
  292. base->ms = RTC_MS_DIV10(tm->tm_ms);
  293. rtc_acts_update_regs();
  294. base->ctrl |= RTC_CTL_CALEN;
  295. irq_unlock(key);
  296. rtc_acts_update_regs();
  297. if(soc_in_sleep_mode()){
  298. soc_udelay(40*1000);
  299. }else{
  300. rtcd->is_set_sync = 1;
  301. rtcd->set_st_ms = k_uptime_get();
  302. }
  303. return 0;
  304. }
  305. static void rtc_acts_set_alarm_interrupt(struct acts_rtc_controller *base, bool enable)
  306. {
  307. if (enable)
  308. base->ctrl |= RTC_CTL_ALIE;
  309. else
  310. base->ctrl &= ~RTC_CTL_ALIE;
  311. rtc_acts_update_regs();
  312. }
  313. /* get & clear alarm irq pending */
  314. static uint32_t rtc_acts_get_pending_int(const struct device *dev)
  315. {
  316. struct acts_rtc_data *rtcd = dev->data;
  317. struct acts_rtc_controller *base = rtcd->base;
  318. int pending;
  319. pending = base->ctrl & RTC_CTL_ALIP;
  320. if (pending) {
  321. /* clear pending */
  322. base->ctrl |= RTC_CTL_ALIP;
  323. LOG_INF("Clear old RTC alarm pending");
  324. rtc_acts_update_regs();
  325. }
  326. return pending;
  327. }
  328. static void rtc_acts_enable(const struct device *dev)
  329. {
  330. struct acts_rtc_data *rtcd = dev->data;
  331. struct acts_rtc_controller *base = rtcd->base;
  332. base->ctrl |= RTC_CTL_CALEN;
  333. rtc_acts_update_regs();
  334. }
  335. static void rtc_acts_disable(const struct device *dev)
  336. {
  337. struct acts_rtc_data *rtcd = dev->data;
  338. struct acts_rtc_controller *base = rtcd->base;
  339. base->ctrl &= ~RTC_CTL_CALEN;
  340. rtc_acts_update_regs();
  341. }
  342. static int rtc_acts_get_time(const struct device *dev, struct rtc_time *tm)
  343. {
  344. struct acts_rtc_data *rtcd = dev->data;
  345. if (!tm)
  346. return -EINVAL;
  347. if (rtc_acts_get_datetime(rtcd, tm)) {
  348. LOG_ERR("failed to get datetime");
  349. return -EACCES;
  350. }
  351. return 0;
  352. }
  353. static void rtc_acts_period_alarm_cb_fn(const void *cb_data);
  354. static int rtc_acts_set_time(const struct device *dev, const struct rtc_time *tm)
  355. {
  356. struct acts_rtc_data *rtcd = dev->data;
  357. int ret;
  358. if (!tm)
  359. return -EINVAL;
  360. if (rtc_valid_tm((struct rtc_time *)tm)) {
  361. LOG_ERR("Bad time structure");
  362. print_rtc_time((struct rtc_time *)tm);
  363. return -ENOEXEC;
  364. }
  365. ret = rtc_acts_set_datetime(rtcd, (struct rtc_time *)tm);
  366. if (ret) {
  367. LOG_ERR("rtc set time error:%d", ret);
  368. return ret;
  369. }
  370. if (rtcd->period_config.cb_fn) {
  371. ret = rtc_acts_set_alarm(dev, NULL, false);
  372. if (!ret)
  373. //ret = __rtc_acts_set_alarm_period(dev);
  374. rtc_acts_period_alarm_cb_fn(dev);
  375. }
  376. return ret;
  377. }
  378. static int rtc_acts_set_alarm_time(const struct acts_rtc_data *rtcd, struct rtc_time *tm)
  379. {
  380. struct acts_rtc_controller *base = rtcd->base;
  381. /* disable alarm interrupt */
  382. rtc_acts_set_alarm_interrupt(base, false);
  383. base->ymd_alarm = RTC_YMD_VAL(tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday);
  384. base->hms_alarm = RTC_HMS_VAL(tm->tm_hour, tm->tm_min, tm->tm_sec);
  385. base->ms_alarm = RTC_MS_DIV10(tm->tm_ms);
  386. rtc_acts_update_regs();
  387. /* enable alarm interrupt */
  388. rtc_acts_set_alarm_interrupt(base, true);
  389. LOG_DBG("set alarm: %04d-%02d-%02d %02d:%02d:%02d:%03d",
  390. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  391. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_ms);
  392. return 0;
  393. }
  394. static int rtc_acts_set_alarm(const struct device *dev, struct rtc_alarm_config *config, bool enable)
  395. {
  396. struct acts_rtc_data *rtcd = dev->data;
  397. int ret = 0;
  398. /* clear old alarm pending */
  399. rtc_acts_get_pending_int(dev);
  400. if (enable) {
  401. if (!config) {
  402. LOG_ERR("no alarm configuration");
  403. return -EINVAL;
  404. }
  405. if (rtc_valid_tm(&config->alarm_time)) {
  406. LOG_ERR("Bad time structure");
  407. print_rtc_time(&config->alarm_time);
  408. return -ENOEXEC;
  409. }
  410. rtcd->alarm_cb_fn = config->cb_fn;
  411. rtcd->cb_data = config->cb_data;
  412. rtcd->alarm_en = true;
  413. ret = rtc_acts_set_alarm_time(rtcd, &config->alarm_time);
  414. } else {
  415. rtc_acts_set_alarm_interrupt(rtcd->base, false);
  416. rtcd->alarm_en = false;
  417. rtcd->alarm_cb_fn = NULL;
  418. rtcd->cb_data = NULL;
  419. }
  420. return ret;
  421. }
  422. static int rtc_acts_get_alarm(const struct device *dev, struct rtc_alarm_status *sts)
  423. {
  424. struct acts_rtc_data *rtcd = dev->data;
  425. struct acts_rtc_controller *base = rtcd->base;
  426. struct rtc_time *tm = &sts->alarm_time;
  427. uint32_t hms;
  428. uint32_t ymd;
  429. uint8_t ms;
  430. if (!sts)
  431. return -EINVAL;
  432. memset(sts, 0, sizeof(struct rtc_alarm_status));
  433. sts->is_on = rtcd->alarm_en;
  434. hms = base->hms_alarm;
  435. tm->tm_sec = RTC_HMS_S(hms); /* rtc second 0-59 */
  436. tm->tm_min = RTC_HMS_M(hms); /* rtc minute 0-59 */
  437. tm->tm_hour = RTC_HMS_H(hms); /* rtc hour 0-23 */
  438. ymd = base->ymd_alarm;
  439. tm->tm_mday = RTC_YMD_D(ymd); /* rtc day 1-31 */
  440. tm->tm_wday = 0;
  441. tm->tm_mon = RTC_YMD_M(ymd) - 1; /* rtc mon 1-12 */
  442. tm->tm_year = 100 + RTC_YMD_Y(ymd); /* rtc year 0-99 */
  443. ms = base->ms_alarm;
  444. tm->tm_ms = RTC_MS_MUL10(ms); /* rtc millisecond 0-999 */
  445. return 0;
  446. }
  447. static int __rtc_acts_set_alarm_period(const struct device *dev)
  448. {
  449. struct acts_rtc_data *rtcd = dev->data;
  450. struct rtc_alarm_config alarm_config = {0};
  451. struct rtc_time tm = {0}, _tm = {0};
  452. uint32_t time, ms, _time;
  453. int ret = -1;
  454. struct rtc_alarm_period_config *config = &rtcd->period_config;
  455. uint32_t timestamp = k_uptime_get_32();
  456. while (1) {
  457. rtc_acts_get_time(dev, &tm);
  458. rtc_tm_to_time(&tm, &time);
  459. if ((tm.tm_ms + config->tm_msec) >= 1000) {
  460. time += 1;
  461. ms = tm.tm_ms + config->tm_msec - 1000;
  462. } else {
  463. ms = tm.tm_ms + config->tm_msec;
  464. }
  465. time += config->tm_sec;
  466. rtc_time_to_tm(time, &tm);
  467. tm.tm_ms = ms;
  468. memcpy(&alarm_config.alarm_time, &tm, sizeof(struct rtc_time));
  469. alarm_config.cb_fn = rtc_acts_period_alarm_cb_fn;
  470. alarm_config.cb_data = (const void *)dev;
  471. ret = rtc_acts_set_alarm(dev, &alarm_config, true);
  472. if (ret)
  473. return ret;
  474. rtc_acts_get_time(dev, &_tm);
  475. rtc_tm_to_time(&_tm, &_time);
  476. //print_rtc_time(&_tm);
  477. if ((_tm.tm_ms <= tm.tm_ms) && (_time == time))
  478. break;
  479. if ((_tm.tm_ms > tm.tm_ms) && (time == (_time + 1)))
  480. break;
  481. if (_time < time)
  482. break;
  483. if ((k_uptime_get_32() - timestamp)
  484. < RTC_CONFIG_PERIOD_ALARM_TIMEOUT_MS) {
  485. LOG_ERR("set period alarm timeout");
  486. return -ETIMEDOUT;
  487. }
  488. }
  489. return 0;
  490. }
  491. static void rtc_acts_period_alarm_cb_fn(const void *cb_data)
  492. {
  493. const struct device *dev = (const struct device *)cb_data;
  494. struct acts_rtc_data *rtcd = dev->data;
  495. if (rtcd->period_config.cb_fn)
  496. rtcd->period_config.cb_fn(rtcd->period_config.cb_data);
  497. __rtc_acts_set_alarm_period(dev);
  498. }
  499. static int rtc_acts_set_period_alarm(const struct device *dev,
  500. struct rtc_alarm_period_config *config, bool enable)
  501. {
  502. struct acts_rtc_data *rtcd = dev->data;
  503. int ret;
  504. if (enable) {
  505. memcpy(&rtcd->period_config, config, sizeof(struct rtc_alarm_period_config));
  506. ret = __rtc_acts_set_alarm_period(dev);
  507. } else {
  508. memset(&rtcd->period_config, 0, sizeof(struct rtc_alarm_period_config));
  509. ret = rtc_acts_set_alarm(dev, NULL, false);
  510. }
  511. return ret;
  512. }
  513. void rtc_acts_isr(const struct device *dev)
  514. {
  515. struct acts_rtc_data *rtcd = dev->data;
  516. struct acts_rtc_controller *base = rtcd->base;
  517. int pending = base->ctrl & RTC_CTL_ALIP;
  518. LOG_DBG("alarm isr ctl:0x%x", base->ctrl);
  519. if (pending)
  520. base->ctrl |= RTC_CTL_ALIP; /* clear pending */
  521. /* disable alarm irq */
  522. rtc_acts_set_alarm_interrupt(rtcd->base, false);
  523. if (pending && rtcd->alarm_en && rtcd->alarm_cb_fn) {
  524. LOG_DBG("call alarm_cb_fn %p", rtcd->alarm_cb_fn);
  525. rtcd->alarm_cb_fn(rtcd->cb_data);
  526. }
  527. }
  528. #if (CONFIG_PM_BACKUP_TIME_FUNCTION_EN == 1)
  529. static int rtc_acts_compensate_datatime(struct acts_rtc_data *rtcd)
  530. {
  531. int ret;
  532. struct sys_pm_backup_time pm_bak_time = {0};
  533. struct rtc_time tm = {0};
  534. uint32_t counter8hz_cycles, interval_cycles, cur_time_sec, ms, use_sec;
  535. #if (CONFIG_RTC_CLK_SOURCE == 2)
  536. uint32_t rc32k_freq;
  537. rc32k_freq = acts_clock_rc32k_set_cal_cyc(200);
  538. printk("cur rc32k_freq=%d\n", rc32k_freq);
  539. #endif
  540. ret = nvram_config_get(CONFIG_PM_BACKUP_TIME_NVRAM_ITEM_NAME,
  541. &pm_bak_time, sizeof(struct sys_pm_backup_time));
  542. if ((ret == sizeof(struct sys_pm_backup_time))
  543. && (pm_bak_time.is_backup_time_valid)) {
  544. ret = soc_pmu_get_counter8hz_cycles(true);
  545. if (ret > 0) {
  546. counter8hz_cycles = ret;
  547. printk("power on current 8hz: %d\n", counter8hz_cycles);
  548. /* counter8hz overflow */
  549. if (counter8hz_cycles < pm_bak_time.counter8hz_cycles) {
  550. interval_cycles = PMU_COUTNER8HZ_MAX - pm_bak_time.counter8hz_cycles + counter8hz_cycles;
  551. } else {
  552. interval_cycles = counter8hz_cycles - pm_bak_time.counter8hz_cycles;
  553. }
  554. cur_time_sec = pm_bak_time.rtc_time_sec;
  555. #if (CONFIG_RTC_CLK_SOURCE == 2)
  556. ms = sys_pmu_8hzcycle_to_ms(interval_cycles, pm_bak_time.rc32k_freq + rc32k_freq);
  557. use_sec = ms/1000;
  558. ms += pm_bak_time.rtc_time_msec + 9; // 9 is align 10
  559. cur_time_sec += ms / 1000;
  560. ms = ms % 1000 ;
  561. #else
  562. use_sec = interval_cycles / 8;
  563. cur_time_sec += use_sec;
  564. ms = (interval_cycles / 8)*125 + pm_bak_time.rtc_time_msec + 9;
  565. if(ms >= 1000){
  566. cur_time_sec++;
  567. ms -= 1000;
  568. }
  569. #endif
  570. uint32_t key = irq_lock();
  571. rtc_time_to_tm(cur_time_sec, &tm);
  572. tm.tm_ms = ms;
  573. rtc_acts_set_datetime(rtcd, &tm);
  574. /* set alarm wakeup flag */
  575. rtcd->alarm_wakeup = true;
  576. irq_unlock(key);
  577. #if (CONFIG_RTC_CLK_SOURCE == 2)
  578. printk("S4 sleep interval: %ds update, bk_rc32k=%d, rc32k_now=%d\n",
  579. use_sec, pm_bak_time.rc32k_freq, rc32k_freq);
  580. #if IS_ENABLED(CONFIG_RTC_ENABLE_CALIBRATION) /*rtc calibration use alarm, recovery user alarm*/
  581. if(pm_bak_time.is_user_alarm_on){
  582. if(pm_bak_time.user_alarm_cycles > counter8hz_cycles){
  583. interval_cycles = pm_bak_time.user_alarm_cycles - counter8hz_cycles;
  584. }else{
  585. interval_cycles = PMU_COUTNER8HZ_MAX - counter8hz_cycles + pm_bak_time.user_alarm_cycles;
  586. }
  587. ms = (uint64_t)interval_cycles*32768*125 /rc32k_freq; // cal real ms
  588. printk("recovery alarm: after %d ms, inter=%d, bk=%d\n", ms, interval_cycles, pm_bak_time.user_alarm_cycles);
  589. soc_pmu_alarm8hz_enable(ms);
  590. }else if(pm_bak_time.is_use_alarm_cal){ //use alarm cal rc32k rtc in system poweroff
  591. printk("cali use alarm: power on need disable alarm\n");
  592. soc_pmu_alarm8hz_disable();
  593. }
  594. #endif
  595. #else
  596. printk("S4 sleep interval: %ds update\n", use_sec);
  597. #endif
  598. print_rtc_time(&tm);
  599. memset(&pm_bak_time, 0, sizeof(struct sys_pm_backup_time));
  600. ret = nvram_config_set(CONFIG_PM_BACKUP_TIME_NVRAM_ITEM_NAME,
  601. &pm_bak_time, sizeof(struct sys_pm_backup_time));
  602. if (ret) {
  603. LOG_ERR("failed to save pm backup time to nvram");
  604. return ret;
  605. }
  606. } else {
  607. LOG_ERR("failed to get counter8hz");
  608. return -EIO;
  609. }
  610. }
  611. return 0;
  612. }
  613. #endif
  614. static int rtc_acts_check_datetime(struct acts_rtc_data *rtcd)
  615. {
  616. struct rtc_time tm;
  617. int ret;
  618. #if (CONFIG_PM_BACKUP_TIME_FUNCTION_EN == 1)
  619. rtc_acts_compensate_datatime(rtcd);
  620. #endif
  621. ret = rtc_acts_get_datetime(rtcd, &tm);
  622. if (ret)
  623. return ret;
  624. if (rtc_valid_tm(&tm) < 0) {
  625. LOG_ERR("Invalid RTC date/time, set to default!");
  626. rtc_time_to_tm(RTC_DEFAULT_INIT_TIME, &tm);
  627. rtc_acts_set_datetime(rtcd, &tm);
  628. }
  629. return 0;
  630. }
  631. static bool rtc_acts_is_alarm_wakeup(const struct device *dev)
  632. {
  633. struct acts_rtc_data *rtcd = dev->data;
  634. if (rtcd->alarm_wakeup)
  635. return true;
  636. else
  637. return false;
  638. }
  639. const struct rtc_driver_api rtc_acts_driver_api = {
  640. .enable = rtc_acts_enable,
  641. .disable = rtc_acts_disable,
  642. .get_time = rtc_acts_get_time,
  643. .set_time = rtc_acts_set_time,
  644. .set_alarm = rtc_acts_set_alarm,
  645. .set_period_alarm = rtc_acts_set_period_alarm,
  646. .get_alarm = rtc_acts_get_alarm,
  647. .is_alarm_wakeup = rtc_acts_is_alarm_wakeup,
  648. };
  649. int rtc_acts_init(const struct device *dev)
  650. {
  651. const struct acts_rtc_config *cfg = dev->config;
  652. struct acts_rtc_data *rtcd = dev->data;
  653. struct acts_rtc_controller *base = cfg->base;
  654. rtcd->base = base;
  655. rtcd->alarm_wakeup = false;
  656. rtc_reg = base;
  657. if (soc_pmu_is_alarm_wakeup())
  658. rtcd->alarm_wakeup = true;
  659. /* By default to disable RTC alarm IRQ */
  660. if (base->ctrl & RTC_CTL_ALIE)
  661. base->ctrl |= RTC_CTL_ALIE;
  662. rtc_acts_init_clksource(dev, rtcd);
  663. /* By default to enable the rtc function */
  664. rtc_acts_enable(dev);
  665. rtc_acts_check_datetime(rtcd);
  666. cfg->irq_config();
  667. rtc_acts_base_init();
  668. printk("rtc initialized\n");
  669. return 0;
  670. }
  671. #ifdef CONFIG_PM_DEVICE
  672. int rtc_pm_control(const struct device *dev, enum pm_device_action action)
  673. {
  674. struct acts_rtc_data *rtcd = dev->data;
  675. uint32_t ms;
  676. switch (action) {
  677. case PM_DEVICE_ACTION_RESUME:
  678. rtcd->syc_st_cycle = k_cycle_get_32();
  679. //rtcd->is_need_sync = 1;
  680. break;
  681. case PM_DEVICE_ACTION_SUSPEND:
  682. rtcd->is_need_sync = 0;
  683. if(rtcd->is_set_sync){
  684. rtcd->is_set_sync = 0;
  685. ms = k_uptime_get() - rtcd->set_st_ms;
  686. if(ms < 40){
  687. printk("rtc wait %d ms\n", 40-ms);
  688. soc_udelay((40-ms)*1000); /*wait time (>3clk(40ms)) to rtc set sync*/
  689. }
  690. }
  691. break;
  692. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  693. if (!soc_get_aod_mode())
  694. rtc_acts_set_alarm_interrupt(rtcd->base, false);
  695. break;
  696. case PM_DEVICE_ACTION_LATE_RESUME:
  697. if (!soc_get_aod_mode())
  698. __rtc_acts_set_alarm_period(dev);
  699. break;
  700. default:
  701. return 0;
  702. }
  703. return 0;
  704. }
  705. #else
  706. #define rtc_pm_control NULL
  707. #endif
  708. static void rtc_acts_irq_config(void);
  709. __act_s2_sleep_data struct acts_rtc_data rtc_acts_ddata;
  710. static const struct acts_rtc_config rtc_acts_cdata = {
  711. .base = (struct acts_rtc_controller *)RTC_REG_BASE,
  712. .rtc_clk_source = CONFIG_RTC_CLK_SOURCE,
  713. .irq_config = rtc_acts_irq_config,
  714. };
  715. DEVICE_DEFINE(rtc0, CONFIG_RTC_0_NAME, rtc_acts_init, rtc_pm_control,
  716. &rtc_acts_ddata, &rtc_acts_cdata,
  717. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
  718. &rtc_acts_driver_api);
  719. static void rtc_acts_irq_config(void)
  720. {
  721. IRQ_CONNECT(IRQ_ID_RTC, CONFIG_RTC_IRQ_PRI,
  722. rtc_acts_isr, DEVICE_GET(rtc0), 0);
  723. irq_enable(IRQ_ID_RTC);
  724. }