drv_rtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <rtthread.h>
  31. #include <rthw.h>
  32. #include <rtdevice.h>
  33. #include <riscv_io.h>
  34. #include <ioremap.h>
  35. #include <time.h>
  36. #include "board.h"
  37. #include "drv_rtc.h"
  38. #undef DBG_TAG
  39. #undef DBG_LVL
  40. #define DBG_TAG "drv_rtc"
  41. #define DBG_LVL DBG_INFO
  42. #include <rtdbg.h>
  43. struct k230_rtc_dev
  44. {
  45. struct rt_device device;
  46. const char *name;
  47. rt_ubase_t base;
  48. size_t size;
  49. int vector;
  50. void (*vector_callback)(void);
  51. };
  52. static void pmu_isolation_rtc(void)
  53. {
  54. /* map pwr base address */
  55. volatile void *reg_pmu_pwr = rt_ioremap((void *)PWR_BASE_ADDR, PWR_IO_SIZE);
  56. uint32_t *addr = (uint32_t *)(reg_pmu_pwr + 0x158); /* pmu power control register */
  57. uint32_t data;
  58. /* disable pmu isolation */
  59. data = *addr;
  60. data &= ~0x20;
  61. *addr = data;
  62. rt_iounmap(reg_pmu_pwr);
  63. /* map pmu base address */
  64. volatile void *reg_pmu = rt_ioremap((void*)PMU_BASE_ADDR, PMU_IO_SIZE);
  65. addr = (uint32_t*)(reg_pmu + 0x48); /* pmu int0 to cpu register */
  66. /* enable int6 int7 */
  67. data = *addr;
  68. data |= 0x06;
  69. *addr = data;
  70. addr = (uint32_t*)(reg_pmu + 0x4c); /* pmu int detect en register */
  71. /* enable int6 rtc alarm detection and int7 rtc tick detection */
  72. data = *addr;
  73. data |= 0x06;
  74. *addr = data;
  75. rt_iounmap(reg_pmu);
  76. }
  77. static int rtc_year_is_leap(int year)
  78. {
  79. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  80. }
  81. static void rtc_timer_set_clock_count_value(struct k230_rtc_dev *dev, uint16_t count)
  82. {
  83. volatile volatile rtc_t *rtc = (rtc_t *)dev->base;
  84. rtc->count.curr_count = count;
  85. rtc->count.sum_count = 0x7FFF;
  86. rtc->int_ctrl.timer_w_en = 1;
  87. rt_thread_mdelay(1);
  88. rtc->int_ctrl.timer_w_en = 0;
  89. rtc->int_ctrl.timer_r_en = 1;
  90. }
  91. static void rtc_interrupt_ctrl_set(struct k230_rtc_dev *dev, rtc_interrupt_mode_t mode)
  92. {
  93. volatile rtc_t *rtc = (rtc_t *)dev->base;
  94. if (mode < RTC_INT_TICK_YEAR)
  95. {
  96. rtc->int_ctrl.year_cmp = 0;
  97. rtc->int_ctrl.month_cmp = 0;
  98. rtc->int_ctrl.day_cmp = 0;
  99. rtc->int_ctrl.week_cmp = 0;
  100. rtc->int_ctrl.hour_cmp = 0;
  101. rtc->int_ctrl.minute_cmp = 0;
  102. rtc->int_ctrl.second_cmp = 0;
  103. if (mode & RTC_INT_ALARM_YEAR)
  104. {
  105. rtc->int_ctrl.year_cmp = 1;
  106. }
  107. if (mode & RTC_INT_ALARM_MONTH)
  108. {
  109. rtc->int_ctrl.month_cmp = 1;
  110. }
  111. if (mode & RTC_INT_ALARM_DAY)
  112. {
  113. rtc->int_ctrl.day_cmp = 1;
  114. }
  115. if (mode & RTC_INT_ALARM_WEEK)
  116. {
  117. rtc->int_ctrl.week_cmp = 1;
  118. }
  119. if (mode & RTC_INT_ALARM_HOUR)
  120. {
  121. rtc->int_ctrl.hour_cmp = 1;
  122. }
  123. if (mode & RTC_INT_ALARM_MINUTE)
  124. {
  125. rtc->int_ctrl.minute_cmp = 1;
  126. }
  127. if (mode & RTC_INT_ALARM_SECOND)
  128. {
  129. rtc->int_ctrl.second_cmp = 1;
  130. }
  131. rtc->int_ctrl.alarm_en = 1;
  132. }
  133. else
  134. {
  135. switch(mode)
  136. {
  137. case RTC_INT_TICK_YEAR:
  138. rtc->int_ctrl.tick_sel = 0x8;
  139. rtc->int_ctrl.tick_en = 1;
  140. break;
  141. case RTC_INT_TICK_MONTH:
  142. rtc->int_ctrl.tick_sel = 0x7;
  143. rtc->int_ctrl.tick_en = 1;
  144. break;
  145. case RTC_INT_TICK_DAY:
  146. rtc->int_ctrl.tick_sel = 0x6;
  147. rtc->int_ctrl.tick_en = 1;
  148. break;
  149. case RTC_INT_TICK_WEEK:
  150. rtc->int_ctrl.tick_sel = 0x5;
  151. rtc->int_ctrl.tick_en = 1;
  152. break;
  153. case RTC_INT_TICK_HOUR:
  154. rtc->int_ctrl.tick_sel = 0x4;
  155. rtc->int_ctrl.tick_en = 1;
  156. break;
  157. case RTC_INT_TICK_MINUTE:
  158. rtc->int_ctrl.tick_sel = 0x3;
  159. rtc->int_ctrl.tick_en = 1;
  160. break;
  161. case RTC_INT_TICK_SECOND:
  162. rtc->int_ctrl.tick_sel = 0x2;
  163. rtc->int_ctrl.tick_en = 1;
  164. break;
  165. case RTC_INT_TICK_S8:
  166. rtc->int_ctrl.tick_sel = 0x1;
  167. rtc->int_ctrl.tick_en = 1;
  168. break;
  169. case RTC_INT_TICK_S64:
  170. rtc->int_ctrl.tick_sel = 0x0;
  171. rtc->int_ctrl.tick_en = 1;
  172. break;
  173. default :
  174. break;
  175. }
  176. }
  177. }
  178. static void rtc_stop_interrupt(struct k230_rtc_dev *dev)
  179. {
  180. rt_hw_interrupt_mask(dev->vector);
  181. }
  182. static void rtc_alarm_stop(struct k230_rtc_dev *dev)
  183. {
  184. volatile rtc_t *rtc = (rtc_t *)dev->base;
  185. rtc->int_ctrl.alarm_en = 0;
  186. rtc_stop_interrupt(dev);
  187. }
  188. static void rtc_tick_stop(struct k230_rtc_dev *dev)
  189. {
  190. volatile rtc_t *rtc = (rtc_t *)dev->base;
  191. rtc->int_ctrl.tick_en = 0;
  192. rtc_stop_interrupt(dev);
  193. }
  194. static void rtc_alarm_clear_interrupt(struct k230_rtc_dev *dev)
  195. {
  196. volatile rtc_t *rtc = (rtc_t *)dev->base;
  197. rtc->int_ctrl.alarm_clr = 1;
  198. }
  199. static void rtc_irq(int vector, void *param)
  200. {
  201. struct k230_rtc_dev *dev = (struct k230_rtc_dev *)param;
  202. rtc_alarm_clear_interrupt(dev);
  203. if (dev->vector_callback != RT_NULL)
  204. {
  205. dev->vector_callback();
  206. }
  207. }
  208. static void rtc_date_time_set(struct k230_rtc_dev *dev, int year, int month, int day, \
  209. int hour, int minute, int second, int week)
  210. {
  211. rtc_date_t date;
  212. rtc_time_t time;
  213. rtc_count_t count;
  214. volatile rtc_t *rtc = (rtc_t *)dev->base;
  215. int val = year % 100;
  216. int year_l, year_h;
  217. if(val == 0)
  218. {
  219. year_l = 100;
  220. year_h = year / 100 - 1;
  221. }
  222. else
  223. {
  224. year_l = val;
  225. year_h = (year - val) / 100;
  226. }
  227. rtc->int_ctrl.timer_w_en = 1;
  228. date.year_h = year_h;
  229. date.year_l = year_l;
  230. date.month = month;
  231. date.day = day;
  232. date.leap_year = rtc_year_is_leap(year);
  233. time.week = week;
  234. time.hour = hour;
  235. time.minute = minute;
  236. time.second = second;
  237. rtc->date = date;
  238. rtc->time = time;
  239. }
  240. static void rtc_timer_get(struct k230_rtc_dev *dev, time_t *t)
  241. {
  242. volatile rtc_t *rtc = (rtc_t *)dev->base;
  243. struct tm tm;
  244. if (rtc->int_ctrl.timer_r_en == 0)
  245. {
  246. rtc->int_ctrl.timer_r_en = 1;
  247. }
  248. tm.tm_sec = rtc->time.second;
  249. tm.tm_min = rtc->time.minute;
  250. tm.tm_hour = rtc->time.hour;
  251. tm.tm_mday = rtc->date.day;
  252. tm.tm_mon = rtc->date.month - 1;
  253. tm.tm_year = (rtc->date.year_h * 100 + rtc->date.year_l) - 1900;
  254. tm.tm_wday = rtc->time.week;
  255. *t = timegm(&tm);
  256. }
  257. static void rtc_timer_set(struct k230_rtc_dev *dev, time_t *t)
  258. {
  259. struct tm p_tm;
  260. gmtime_r(t, &p_tm);
  261. rtc_date_time_set(dev, (p_tm.tm_year + 1900), p_tm.tm_mon + 1, p_tm.tm_mday, \
  262. p_tm.tm_hour, p_tm.tm_min, p_tm.tm_sec, p_tm.tm_wday);
  263. rtc_timer_set_clock_count_value(dev, 0);
  264. }
  265. static void rtc_alarm_get(struct k230_rtc_dev *dev, void *args)
  266. {
  267. struct tm *tm = (struct tm*)args;
  268. volatile rtc_t *rtc = (rtc_t *)dev->base;
  269. rtc_alarm_date_t alarm_date = rtc->alarm_date;
  270. rtc_alarm_time_t alarm_time = rtc->alarm_time;
  271. tm->tm_year = (alarm_date.alarm_year_h * 100 + alarm_date.alarm_year_l) -1900;
  272. tm->tm_mon = alarm_date.alarm_month - 1;
  273. tm->tm_mday = alarm_date.alarm_day;
  274. tm->tm_hour = alarm_time.alarm_hour;
  275. tm->tm_min = alarm_time.alarm_minute;
  276. tm->tm_sec = alarm_time.alarm_second;
  277. }
  278. static void rtc_alarm_set(struct k230_rtc_dev *dev, void *args)
  279. {
  280. rtc_alarm_setup_t *setup = (rtc_alarm_setup_t *)args;
  281. struct tm tm = setup->tm;
  282. time_t t;
  283. struct tm p_tm;
  284. volatile rtc_t *rtc = (rtc_t *)dev->base;
  285. rtc_alarm_time_t alarm_time;
  286. rtc_alarm_date_t alarm_date;
  287. rtc_date_t date = rtc->date;
  288. int year, year_l, year_h, val;
  289. t = mktime(&tm);
  290. gmtime_r(&t, &p_tm);
  291. year = p_tm.tm_year + 1900;
  292. val = year % 100;
  293. if(val == 0)
  294. {
  295. year_l = 100;
  296. year_h = year / 100 - 1;
  297. }
  298. else
  299. {
  300. year_l = val;
  301. year_h = (year - val) / 100;
  302. }
  303. alarm_date.alarm_year_h = year_h;
  304. alarm_date.alarm_year_l = year_l;
  305. alarm_date.alarm_month = p_tm.tm_mon + 1;
  306. alarm_date.alarm_day = p_tm.tm_mday;
  307. alarm_time.alarm_hour = p_tm.tm_hour;
  308. alarm_time.alarm_minute = p_tm.tm_min;
  309. alarm_time.alarm_second = p_tm.tm_sec;
  310. rtc->alarm_date = alarm_date;
  311. rtc->alarm_time = alarm_time;
  312. rtc_alarm_clear_interrupt(dev);
  313. rt_hw_interrupt_install(dev->vector, rtc_irq, dev, "rtc");
  314. rt_hw_interrupt_umask(dev->vector);
  315. rtc_interrupt_ctrl_set(dev, setup->flag);
  316. }
  317. static rt_err_t rtc_device_init(rt_device_t dev)
  318. {
  319. struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
  320. rtc_alarm_stop(rtc_dev);
  321. rtc_tick_stop(rtc_dev);
  322. return RT_EOK;
  323. }
  324. static rt_err_t rtc_device_open(rt_device_t dev, rt_uint16_t oflag)
  325. {
  326. return RT_EOK;
  327. }
  328. static rt_err_t rtc_device_close(rt_device_t dev)
  329. {
  330. return RT_EOK;
  331. }
  332. static rt_ssize_t rtc_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  333. {
  334. time_t t;
  335. struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
  336. rtc_timer_get(rtc_dev, &t);
  337. rt_memcpy(buffer, (void*)&t, sizeof(t));
  338. return size;
  339. }
  340. static rt_ssize_t rtc_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  341. {
  342. struct tm *tm = (struct tm*)buffer;
  343. time_t t = mktime(tm);
  344. struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
  345. rtc_timer_set(rtc_dev, &t);
  346. return size;
  347. }
  348. static rt_err_t rtc_device_control(rt_device_t dev, int cmd, void *args)
  349. {
  350. time_t time;
  351. RT_ASSERT(dev != RT_NULL);
  352. struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
  353. RT_ASSERT(rtc_dev != RT_NULL);
  354. switch (cmd)
  355. {
  356. case RT_DEVICE_CTRL_RTC_GET_TIME:
  357. rtc_timer_get(rtc_dev, (time_t*)args);
  358. break;
  359. case RT_DEVICE_CTRL_RTC_SET_TIME:
  360. rtc_timer_set(rtc_dev, (time_t *)args);
  361. break;
  362. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  363. rtc_alarm_get(rtc_dev, args);
  364. break;
  365. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  366. rtc_alarm_set(rtc_dev, args);
  367. break;
  368. case RT_DEVICE_CTRL_RTC_STOP_ALARM:
  369. rtc_alarm_stop(rtc_dev);
  370. break;
  371. case RT_DEVICE_CTRL_RTC_STOP_TICK:
  372. rtc_tick_stop(rtc_dev);
  373. break;
  374. case RT_DEVICE_CTRL_RTC_SET_CALLBACK:
  375. rtc_dev->vector_callback = args;
  376. break;
  377. default:
  378. return -RT_EINVAL;
  379. }
  380. return RT_EOK;
  381. }
  382. const static struct rt_device_ops rtc_ops =
  383. {
  384. .init = rtc_device_init,
  385. .open = rtc_device_open,
  386. .close = rtc_device_close,
  387. .read = rtc_device_read,
  388. .write = rtc_device_write,
  389. .control = rtc_device_control,
  390. };
  391. static struct k230_rtc_dev rtc_dev =
  392. {
  393. .name = "rtc",
  394. .base = RTC_BASE_ADDR,
  395. .size = RTC_IO_SIZE,
  396. .vector = K230_IRQ_PMU,
  397. .vector_callback = RT_NULL,
  398. };
  399. static int rt_hw_rtc_init(void)
  400. {
  401. rt_err_t ret;
  402. pmu_isolation_rtc();
  403. rtc_dev.device.type = RT_Device_Class_RTC;
  404. rtc_dev.device.rx_indicate = RT_NULL;
  405. rtc_dev.device.tx_complete = RT_NULL;
  406. #ifdef RT_USING_DEVICE_OPS
  407. rtc_dev.device.ops = &rtc_ops;
  408. #else
  409. rtc_dev.device.init = rtc_device_init;
  410. rtc_dev.device.open = rtc_device_open;
  411. rtc_dev.device.close = rtc_device_close;
  412. rtc_dev.device.read = rtc_device_read;
  413. rtc_dev.device.write = rtc_device_write;
  414. rtc_dev.device.control = rtc_device_control;
  415. #endif /* RT_USING_DEVICE_OPS */
  416. rtc_dev.device.user_data = RT_NULL;
  417. rtc_dev.base = (rt_ubase_t)rt_ioremap((void *)rtc_dev.base, rtc_dev.size);
  418. RT_ASSERT(rtc_dev.base != RT_NULL);
  419. ret = rt_device_register(&rtc_dev.device, "rtc", RT_DEVICE_FLAG_RDWR);
  420. RT_ASSERT(ret == RT_EOK);
  421. LOG_I("rtc driver register OK\n");
  422. rtc_alarm_stop(&rtc_dev);
  423. rtc_tick_stop(&rtc_dev);
  424. return ret;
  425. }
  426. INIT_DEVICE_EXPORT(rt_hw_rtc_init);