exynos4210_rtc.c 16 KB

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