exynos4210_rtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Samsung exynos4210 Real Time Clock
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * Ogurtsov Oleg <o.ogurtsov@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /* Description:
  22. * Register RTCCON:
  23. * CLKSEL Bit[1] not used
  24. * CLKOUTEN Bit[9] not used
  25. */
  26. #include "sysbus.h"
  27. #include "qemu/timer.h"
  28. #include "qemu-common.h"
  29. #include "ptimer.h"
  30. #include "hw.h"
  31. #include "qemu/timer.h"
  32. #include "sysemu/sysemu.h"
  33. #include "exynos4210.h"
  34. #define DEBUG_RTC 0
  35. #if DEBUG_RTC
  36. #define DPRINTF(fmt, ...) \
  37. do { fprintf(stdout, "RTC: [%24s:%5d] " fmt, __func__, __LINE__, \
  38. ## __VA_ARGS__); } while (0)
  39. #else
  40. #define DPRINTF(fmt, ...) do {} while (0)
  41. #endif
  42. #define EXYNOS4210_RTC_REG_MEM_SIZE 0x0100
  43. #define INTP 0x0030
  44. #define RTCCON 0x0040
  45. #define TICCNT 0x0044
  46. #define RTCALM 0x0050
  47. #define ALMSEC 0x0054
  48. #define ALMMIN 0x0058
  49. #define ALMHOUR 0x005C
  50. #define ALMDAY 0x0060
  51. #define ALMMON 0x0064
  52. #define ALMYEAR 0x0068
  53. #define BCDSEC 0x0070
  54. #define BCDMIN 0x0074
  55. #define BCDHOUR 0x0078
  56. #define BCDDAY 0x007C
  57. #define BCDDAYWEEK 0x0080
  58. #define BCDMON 0x0084
  59. #define BCDYEAR 0x0088
  60. #define CURTICNT 0x0090
  61. #define TICK_TIMER_ENABLE 0x0100
  62. #define TICNT_THRESHHOLD 2
  63. #define RTC_ENABLE 0x0001
  64. #define INTP_TICK_ENABLE 0x0001
  65. #define INTP_ALM_ENABLE 0x0002
  66. #define ALARM_INT_ENABLE 0x0040
  67. #define RTC_BASE_FREQ 32768
  68. typedef struct Exynos4210RTCState {
  69. SysBusDevice busdev;
  70. MemoryRegion iomem;
  71. /* registers */
  72. uint32_t reg_intp;
  73. uint32_t reg_rtccon;
  74. uint32_t reg_ticcnt;
  75. uint32_t reg_rtcalm;
  76. uint32_t reg_almsec;
  77. uint32_t reg_almmin;
  78. uint32_t reg_almhour;
  79. uint32_t reg_almday;
  80. uint32_t reg_almmon;
  81. uint32_t reg_almyear;
  82. uint32_t reg_curticcnt;
  83. ptimer_state *ptimer; /* tick timer */
  84. ptimer_state *ptimer_1Hz; /* clock timer */
  85. uint32_t freq;
  86. qemu_irq tick_irq; /* Time Tick Generator irq */
  87. qemu_irq alm_irq; /* alarm irq */
  88. struct tm current_tm; /* current time */
  89. } Exynos4210RTCState;
  90. #define TICCKSEL(value) ((value & (0x0F << 4)) >> 4)
  91. /*** VMState ***/
  92. static const VMStateDescription vmstate_exynos4210_rtc_state = {
  93. .name = "exynos4210.rtc",
  94. .version_id = 1,
  95. .minimum_version_id = 1,
  96. .minimum_version_id_old = 1,
  97. .fields = (VMStateField[]) {
  98. VMSTATE_UINT32(reg_intp, Exynos4210RTCState),
  99. VMSTATE_UINT32(reg_rtccon, Exynos4210RTCState),
  100. VMSTATE_UINT32(reg_ticcnt, Exynos4210RTCState),
  101. VMSTATE_UINT32(reg_rtcalm, Exynos4210RTCState),
  102. VMSTATE_UINT32(reg_almsec, Exynos4210RTCState),
  103. VMSTATE_UINT32(reg_almmin, Exynos4210RTCState),
  104. VMSTATE_UINT32(reg_almhour, Exynos4210RTCState),
  105. VMSTATE_UINT32(reg_almday, Exynos4210RTCState),
  106. VMSTATE_UINT32(reg_almmon, Exynos4210RTCState),
  107. VMSTATE_UINT32(reg_almyear, Exynos4210RTCState),
  108. VMSTATE_UINT32(reg_curticcnt, Exynos4210RTCState),
  109. VMSTATE_PTIMER(ptimer, Exynos4210RTCState),
  110. VMSTATE_PTIMER(ptimer_1Hz, Exynos4210RTCState),
  111. VMSTATE_UINT32(freq, Exynos4210RTCState),
  112. VMSTATE_INT32(current_tm.tm_sec, Exynos4210RTCState),
  113. VMSTATE_INT32(current_tm.tm_min, Exynos4210RTCState),
  114. VMSTATE_INT32(current_tm.tm_hour, Exynos4210RTCState),
  115. VMSTATE_INT32(current_tm.tm_wday, Exynos4210RTCState),
  116. VMSTATE_INT32(current_tm.tm_mday, Exynos4210RTCState),
  117. VMSTATE_INT32(current_tm.tm_mon, Exynos4210RTCState),
  118. VMSTATE_INT32(current_tm.tm_year, Exynos4210RTCState),
  119. VMSTATE_END_OF_LIST()
  120. }
  121. };
  122. #define BCD3DIGITS(x) \
  123. ((uint32_t)to_bcd((uint8_t)(x % 100)) + \
  124. ((uint32_t)to_bcd((uint8_t)((x % 1000) / 100)) << 8))
  125. static void check_alarm_raise(Exynos4210RTCState *s)
  126. {
  127. unsigned int alarm_raise = 0;
  128. struct tm stm = s->current_tm;
  129. if ((s->reg_rtcalm & 0x01) &&
  130. (to_bcd((uint8_t)stm.tm_sec) == (uint8_t)s->reg_almsec)) {
  131. alarm_raise = 1;
  132. }
  133. if ((s->reg_rtcalm & 0x02) &&
  134. (to_bcd((uint8_t)stm.tm_min) == (uint8_t)s->reg_almmin)) {
  135. alarm_raise = 1;
  136. }
  137. if ((s->reg_rtcalm & 0x04) &&
  138. (to_bcd((uint8_t)stm.tm_hour) == (uint8_t)s->reg_almhour)) {
  139. alarm_raise = 1;
  140. }
  141. if ((s->reg_rtcalm & 0x08) &&
  142. (to_bcd((uint8_t)stm.tm_mday) == (uint8_t)s->reg_almday)) {
  143. alarm_raise = 1;
  144. }
  145. if ((s->reg_rtcalm & 0x10) &&
  146. (to_bcd((uint8_t)stm.tm_mon) == (uint8_t)s->reg_almmon)) {
  147. alarm_raise = 1;
  148. }
  149. if ((s->reg_rtcalm & 0x20) &&
  150. (BCD3DIGITS(stm.tm_year) == s->reg_almyear)) {
  151. alarm_raise = 1;
  152. }
  153. if (alarm_raise) {
  154. DPRINTF("ALARM IRQ\n");
  155. /* set irq status */
  156. s->reg_intp |= INTP_ALM_ENABLE;
  157. qemu_irq_raise(s->alm_irq);
  158. }
  159. }
  160. /*
  161. * RTC update frequency
  162. * Parameters:
  163. * reg_value - current RTCCON register or his new value
  164. */
  165. static void exynos4210_rtc_update_freq(Exynos4210RTCState *s,
  166. uint32_t reg_value)
  167. {
  168. uint32_t freq;
  169. freq = s->freq;
  170. /* set frequncy for time generator */
  171. s->freq = RTC_BASE_FREQ / (1 << TICCKSEL(reg_value));
  172. if (freq != s->freq) {
  173. ptimer_set_freq(s->ptimer, s->freq);
  174. DPRINTF("freq=%dHz\n", s->freq);
  175. }
  176. }
  177. /* month is between 0 and 11. */
  178. static int get_days_in_month(int month, int year)
  179. {
  180. static const int days_tab[12] = {
  181. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  182. };
  183. int d;
  184. if ((unsigned)month >= 12) {
  185. return 31;
  186. }
  187. d = days_tab[month];
  188. if (month == 1) {
  189. if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) {
  190. d++;
  191. }
  192. }
  193. return d;
  194. }
  195. /* update 'tm' to the next second */
  196. static void rtc_next_second(struct tm *tm)
  197. {
  198. int days_in_month;
  199. tm->tm_sec++;
  200. if ((unsigned)tm->tm_sec >= 60) {
  201. tm->tm_sec = 0;
  202. tm->tm_min++;
  203. if ((unsigned)tm->tm_min >= 60) {
  204. tm->tm_min = 0;
  205. tm->tm_hour++;
  206. if ((unsigned)tm->tm_hour >= 24) {
  207. tm->tm_hour = 0;
  208. /* next day */
  209. tm->tm_wday++;
  210. if ((unsigned)tm->tm_wday >= 7) {
  211. tm->tm_wday = 0;
  212. }
  213. days_in_month = get_days_in_month(tm->tm_mon,
  214. tm->tm_year + 1900);
  215. tm->tm_mday++;
  216. if (tm->tm_mday < 1) {
  217. tm->tm_mday = 1;
  218. } else if (tm->tm_mday > days_in_month) {
  219. tm->tm_mday = 1;
  220. tm->tm_mon++;
  221. if (tm->tm_mon >= 12) {
  222. tm->tm_mon = 0;
  223. tm->tm_year++;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. /*
  231. * tick handler
  232. */
  233. static void exynos4210_rtc_tick(void *opaque)
  234. {
  235. Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
  236. DPRINTF("TICK IRQ\n");
  237. /* set irq status */
  238. s->reg_intp |= INTP_TICK_ENABLE;
  239. /* raise IRQ */
  240. qemu_irq_raise(s->tick_irq);
  241. /* restart timer */
  242. ptimer_set_count(s->ptimer, s->reg_ticcnt);
  243. ptimer_run(s->ptimer, 1);
  244. }
  245. /*
  246. * 1Hz clock handler
  247. */
  248. static void exynos4210_rtc_1Hz_tick(void *opaque)
  249. {
  250. Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
  251. rtc_next_second(&s->current_tm);
  252. /* DPRINTF("1Hz tick\n"); */
  253. /* raise IRQ */
  254. if (s->reg_rtcalm & ALARM_INT_ENABLE) {
  255. check_alarm_raise(s);
  256. }
  257. ptimer_set_count(s->ptimer_1Hz, RTC_BASE_FREQ);
  258. ptimer_run(s->ptimer_1Hz, 1);
  259. }
  260. /*
  261. * RTC Read
  262. */
  263. static uint64_t exynos4210_rtc_read(void *opaque, hwaddr offset,
  264. unsigned size)
  265. {
  266. uint32_t value = 0;
  267. Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
  268. switch (offset) {
  269. case INTP:
  270. value = s->reg_intp;
  271. break;
  272. case RTCCON:
  273. value = s->reg_rtccon;
  274. break;
  275. case TICCNT:
  276. value = s->reg_ticcnt;
  277. break;
  278. case RTCALM:
  279. value = s->reg_rtcalm;
  280. break;
  281. case ALMSEC:
  282. value = s->reg_almsec;
  283. break;
  284. case ALMMIN:
  285. value = s->reg_almmin;
  286. break;
  287. case ALMHOUR:
  288. value = s->reg_almhour;
  289. break;
  290. case ALMDAY:
  291. value = s->reg_almday;
  292. break;
  293. case ALMMON:
  294. value = s->reg_almmon;
  295. break;
  296. case ALMYEAR:
  297. value = s->reg_almyear;
  298. break;
  299. case BCDSEC:
  300. value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_sec);
  301. break;
  302. case BCDMIN:
  303. value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_min);
  304. break;
  305. case BCDHOUR:
  306. value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_hour);
  307. break;
  308. case BCDDAYWEEK:
  309. value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_wday);
  310. break;
  311. case BCDDAY:
  312. value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_mday);
  313. break;
  314. case BCDMON:
  315. value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_mon + 1);
  316. break;
  317. case BCDYEAR:
  318. value = BCD3DIGITS(s->current_tm.tm_year);
  319. break;
  320. case CURTICNT:
  321. s->reg_curticcnt = ptimer_get_count(s->ptimer);
  322. value = s->reg_curticcnt;
  323. break;
  324. default:
  325. fprintf(stderr,
  326. "[exynos4210.rtc: bad read offset " TARGET_FMT_plx "]\n",
  327. offset);
  328. break;
  329. }
  330. return value;
  331. }
  332. /*
  333. * RTC Write
  334. */
  335. static void exynos4210_rtc_write(void *opaque, hwaddr offset,
  336. uint64_t value, unsigned size)
  337. {
  338. Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
  339. switch (offset) {
  340. case INTP:
  341. if (value & INTP_ALM_ENABLE) {
  342. qemu_irq_lower(s->alm_irq);
  343. s->reg_intp &= (~INTP_ALM_ENABLE);
  344. }
  345. if (value & INTP_TICK_ENABLE) {
  346. qemu_irq_lower(s->tick_irq);
  347. s->reg_intp &= (~INTP_TICK_ENABLE);
  348. }
  349. break;
  350. case RTCCON:
  351. if (value & RTC_ENABLE) {
  352. exynos4210_rtc_update_freq(s, value);
  353. }
  354. if ((value & RTC_ENABLE) > (s->reg_rtccon & RTC_ENABLE)) {
  355. /* clock timer */
  356. ptimer_set_count(s->ptimer_1Hz, RTC_BASE_FREQ);
  357. ptimer_run(s->ptimer_1Hz, 1);
  358. DPRINTF("run clock timer\n");
  359. }
  360. if ((value & RTC_ENABLE) < (s->reg_rtccon & RTC_ENABLE)) {
  361. /* tick timer */
  362. ptimer_stop(s->ptimer);
  363. /* clock timer */
  364. ptimer_stop(s->ptimer_1Hz);
  365. DPRINTF("stop all timers\n");
  366. }
  367. if (value & RTC_ENABLE) {
  368. if ((value & TICK_TIMER_ENABLE) >
  369. (s->reg_rtccon & TICK_TIMER_ENABLE) &&
  370. (s->reg_ticcnt)) {
  371. ptimer_set_count(s->ptimer, s->reg_ticcnt);
  372. ptimer_run(s->ptimer, 1);
  373. DPRINTF("run tick timer\n");
  374. }
  375. if ((value & TICK_TIMER_ENABLE) <
  376. (s->reg_rtccon & TICK_TIMER_ENABLE)) {
  377. ptimer_stop(s->ptimer);
  378. }
  379. }
  380. s->reg_rtccon = value;
  381. break;
  382. case TICCNT:
  383. if (value > TICNT_THRESHHOLD) {
  384. s->reg_ticcnt = value;
  385. } else {
  386. fprintf(stderr,
  387. "[exynos4210.rtc: bad TICNT value %u ]\n",
  388. (uint32_t)value);
  389. }
  390. break;
  391. case RTCALM:
  392. s->reg_rtcalm = value;
  393. break;
  394. case ALMSEC:
  395. s->reg_almsec = (value & 0x7f);
  396. break;
  397. case ALMMIN:
  398. s->reg_almmin = (value & 0x7f);
  399. break;
  400. case ALMHOUR:
  401. s->reg_almhour = (value & 0x3f);
  402. break;
  403. case ALMDAY:
  404. s->reg_almday = (value & 0x3f);
  405. break;
  406. case ALMMON:
  407. s->reg_almmon = (value & 0x1f);
  408. break;
  409. case ALMYEAR:
  410. s->reg_almyear = (value & 0x0fff);
  411. break;
  412. case BCDSEC:
  413. if (s->reg_rtccon & RTC_ENABLE) {
  414. s->current_tm.tm_sec = (int)from_bcd((uint8_t)value);
  415. }
  416. break;
  417. case BCDMIN:
  418. if (s->reg_rtccon & RTC_ENABLE) {
  419. s->current_tm.tm_min = (int)from_bcd((uint8_t)value);
  420. }
  421. break;
  422. case BCDHOUR:
  423. if (s->reg_rtccon & RTC_ENABLE) {
  424. s->current_tm.tm_hour = (int)from_bcd((uint8_t)value);
  425. }
  426. break;
  427. case BCDDAYWEEK:
  428. if (s->reg_rtccon & RTC_ENABLE) {
  429. s->current_tm.tm_wday = (int)from_bcd((uint8_t)value);
  430. }
  431. break;
  432. case BCDDAY:
  433. if (s->reg_rtccon & RTC_ENABLE) {
  434. s->current_tm.tm_mday = (int)from_bcd((uint8_t)value);
  435. }
  436. break;
  437. case BCDMON:
  438. if (s->reg_rtccon & RTC_ENABLE) {
  439. s->current_tm.tm_mon = (int)from_bcd((uint8_t)value) - 1;
  440. }
  441. break;
  442. case BCDYEAR:
  443. if (s->reg_rtccon & RTC_ENABLE) {
  444. /* 3 digits */
  445. s->current_tm.tm_year = (int)from_bcd((uint8_t)value) +
  446. (int)from_bcd((uint8_t)((value >> 8) & 0x0f)) * 100;
  447. }
  448. break;
  449. default:
  450. fprintf(stderr,
  451. "[exynos4210.rtc: bad write offset " TARGET_FMT_plx "]\n",
  452. offset);
  453. break;
  454. }
  455. }
  456. /*
  457. * Set default values to timer fields and registers
  458. */
  459. static void exynos4210_rtc_reset(DeviceState *d)
  460. {
  461. Exynos4210RTCState *s = (Exynos4210RTCState *)d;
  462. qemu_get_timedate(&s->current_tm, 0);
  463. DPRINTF("Get time from host: %d-%d-%d %2d:%02d:%02d\n",
  464. s->current_tm.tm_year, s->current_tm.tm_mon, s->current_tm.tm_mday,
  465. s->current_tm.tm_hour, s->current_tm.tm_min, s->current_tm.tm_sec);
  466. s->reg_intp = 0;
  467. s->reg_rtccon = 0;
  468. s->reg_ticcnt = 0;
  469. s->reg_rtcalm = 0;
  470. s->reg_almsec = 0;
  471. s->reg_almmin = 0;
  472. s->reg_almhour = 0;
  473. s->reg_almday = 0;
  474. s->reg_almmon = 0;
  475. s->reg_almyear = 0;
  476. s->reg_curticcnt = 0;
  477. exynos4210_rtc_update_freq(s, s->reg_rtccon);
  478. ptimer_stop(s->ptimer);
  479. ptimer_stop(s->ptimer_1Hz);
  480. }
  481. static const MemoryRegionOps exynos4210_rtc_ops = {
  482. .read = exynos4210_rtc_read,
  483. .write = exynos4210_rtc_write,
  484. .endianness = DEVICE_NATIVE_ENDIAN,
  485. };
  486. /*
  487. * RTC timer initialization
  488. */
  489. static int exynos4210_rtc_init(SysBusDevice *dev)
  490. {
  491. Exynos4210RTCState *s = FROM_SYSBUS(Exynos4210RTCState, dev);
  492. QEMUBH *bh;
  493. bh = qemu_bh_new(exynos4210_rtc_tick, s);
  494. s->ptimer = ptimer_init(bh);
  495. ptimer_set_freq(s->ptimer, RTC_BASE_FREQ);
  496. exynos4210_rtc_update_freq(s, 0);
  497. bh = qemu_bh_new(exynos4210_rtc_1Hz_tick, s);
  498. s->ptimer_1Hz = ptimer_init(bh);
  499. ptimer_set_freq(s->ptimer_1Hz, RTC_BASE_FREQ);
  500. sysbus_init_irq(dev, &s->alm_irq);
  501. sysbus_init_irq(dev, &s->tick_irq);
  502. memory_region_init_io(&s->iomem, &exynos4210_rtc_ops, s, "exynos4210-rtc",
  503. EXYNOS4210_RTC_REG_MEM_SIZE);
  504. sysbus_init_mmio(dev, &s->iomem);
  505. return 0;
  506. }
  507. static void exynos4210_rtc_class_init(ObjectClass *klass, void *data)
  508. {
  509. DeviceClass *dc = DEVICE_CLASS(klass);
  510. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  511. k->init = exynos4210_rtc_init;
  512. dc->reset = exynos4210_rtc_reset;
  513. dc->vmsd = &vmstate_exynos4210_rtc_state;
  514. }
  515. static const TypeInfo exynos4210_rtc_info = {
  516. .name = "exynos4210.rtc",
  517. .parent = TYPE_SYS_BUS_DEVICE,
  518. .instance_size = sizeof(Exynos4210RTCState),
  519. .class_init = exynos4210_rtc_class_init,
  520. };
  521. static void exynos4210_rtc_register_types(void)
  522. {
  523. type_register_static(&exynos4210_rtc_info);
  524. }
  525. type_init(exynos4210_rtc_register_types)