mc146818rtc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * QEMU MC146818 RTC emulation
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "qemu-timer.h"
  26. #include "sysemu.h"
  27. #include "mc146818rtc.h"
  28. #ifdef TARGET_I386
  29. #include "apic.h"
  30. #endif
  31. //#define DEBUG_CMOS
  32. //#define DEBUG_COALESCED
  33. #ifdef DEBUG_CMOS
  34. # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  35. #else
  36. # define CMOS_DPRINTF(format, ...) do { } while (0)
  37. #endif
  38. #ifdef DEBUG_COALESCED
  39. # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
  40. #else
  41. # define DPRINTF_C(format, ...) do { } while (0)
  42. #endif
  43. #define RTC_REINJECT_ON_ACK_COUNT 20
  44. typedef struct RTCState {
  45. ISADevice dev;
  46. MemoryRegion io;
  47. uint8_t cmos_data[128];
  48. uint8_t cmos_index;
  49. struct tm current_tm;
  50. int32_t base_year;
  51. qemu_irq irq;
  52. qemu_irq sqw_irq;
  53. int it_shift;
  54. /* periodic timer */
  55. QEMUTimer *periodic_timer;
  56. int64_t next_periodic_time;
  57. /* second update */
  58. int64_t next_second_time;
  59. uint16_t irq_reinject_on_ack_count;
  60. uint32_t irq_coalesced;
  61. uint32_t period;
  62. QEMUTimer *coalesced_timer;
  63. QEMUTimer *second_timer;
  64. QEMUTimer *second_timer2;
  65. Notifier clock_reset_notifier;
  66. LostTickPolicy lost_tick_policy;
  67. Notifier suspend_notifier;
  68. } RTCState;
  69. static void rtc_set_time(RTCState *s);
  70. static void rtc_copy_date(RTCState *s);
  71. #ifdef TARGET_I386
  72. static void rtc_coalesced_timer_update(RTCState *s)
  73. {
  74. if (s->irq_coalesced == 0) {
  75. qemu_del_timer(s->coalesced_timer);
  76. } else {
  77. /* divide each RTC interval to 2 - 8 smaller intervals */
  78. int c = MIN(s->irq_coalesced, 7) + 1;
  79. int64_t next_clock = qemu_get_clock_ns(rtc_clock) +
  80. muldiv64(s->period / c, get_ticks_per_sec(), 32768);
  81. qemu_mod_timer(s->coalesced_timer, next_clock);
  82. }
  83. }
  84. static void rtc_coalesced_timer(void *opaque)
  85. {
  86. RTCState *s = opaque;
  87. if (s->irq_coalesced != 0) {
  88. apic_reset_irq_delivered();
  89. s->cmos_data[RTC_REG_C] |= 0xc0;
  90. DPRINTF_C("cmos: injecting from timer\n");
  91. qemu_irq_raise(s->irq);
  92. if (apic_get_irq_delivered()) {
  93. s->irq_coalesced--;
  94. DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
  95. s->irq_coalesced);
  96. }
  97. }
  98. rtc_coalesced_timer_update(s);
  99. }
  100. #endif
  101. static void rtc_timer_update(RTCState *s, int64_t current_time)
  102. {
  103. int period_code, period;
  104. int64_t cur_clock, next_irq_clock;
  105. period_code = s->cmos_data[RTC_REG_A] & 0x0f;
  106. if (period_code != 0
  107. && ((s->cmos_data[RTC_REG_B] & REG_B_PIE)
  108. || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) {
  109. if (period_code <= 2)
  110. period_code += 7;
  111. /* period in 32 Khz cycles */
  112. period = 1 << (period_code - 1);
  113. #ifdef TARGET_I386
  114. if (period != s->period) {
  115. s->irq_coalesced = (s->irq_coalesced * s->period) / period;
  116. DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
  117. }
  118. s->period = period;
  119. #endif
  120. /* compute 32 khz clock */
  121. cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
  122. next_irq_clock = (cur_clock & ~(period - 1)) + period;
  123. s->next_periodic_time =
  124. muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1;
  125. qemu_mod_timer(s->periodic_timer, s->next_periodic_time);
  126. } else {
  127. #ifdef TARGET_I386
  128. s->irq_coalesced = 0;
  129. #endif
  130. qemu_del_timer(s->periodic_timer);
  131. }
  132. }
  133. static void rtc_periodic_timer(void *opaque)
  134. {
  135. RTCState *s = opaque;
  136. rtc_timer_update(s, s->next_periodic_time);
  137. s->cmos_data[RTC_REG_C] |= REG_C_PF;
  138. if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
  139. s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
  140. #ifdef TARGET_I386
  141. if (s->lost_tick_policy == LOST_TICK_SLEW) {
  142. if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
  143. s->irq_reinject_on_ack_count = 0;
  144. apic_reset_irq_delivered();
  145. qemu_irq_raise(s->irq);
  146. if (!apic_get_irq_delivered()) {
  147. s->irq_coalesced++;
  148. rtc_coalesced_timer_update(s);
  149. DPRINTF_C("cmos: coalesced irqs increased to %d\n",
  150. s->irq_coalesced);
  151. }
  152. } else
  153. #endif
  154. qemu_irq_raise(s->irq);
  155. }
  156. if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
  157. /* Not square wave at all but we don't want 2048Hz interrupts!
  158. Must be seen as a pulse. */
  159. qemu_irq_raise(s->sqw_irq);
  160. }
  161. }
  162. static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
  163. {
  164. RTCState *s = opaque;
  165. if ((addr & 1) == 0) {
  166. s->cmos_index = data & 0x7f;
  167. } else {
  168. CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
  169. s->cmos_index, data);
  170. switch(s->cmos_index) {
  171. case RTC_SECONDS_ALARM:
  172. case RTC_MINUTES_ALARM:
  173. case RTC_HOURS_ALARM:
  174. s->cmos_data[s->cmos_index] = data;
  175. break;
  176. case RTC_SECONDS:
  177. case RTC_MINUTES:
  178. case RTC_HOURS:
  179. case RTC_DAY_OF_WEEK:
  180. case RTC_DAY_OF_MONTH:
  181. case RTC_MONTH:
  182. case RTC_YEAR:
  183. s->cmos_data[s->cmos_index] = data;
  184. /* if in set mode, do not update the time */
  185. if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
  186. rtc_set_time(s);
  187. }
  188. break;
  189. case RTC_REG_A:
  190. /* UIP bit is read only */
  191. s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
  192. (s->cmos_data[RTC_REG_A] & REG_A_UIP);
  193. rtc_timer_update(s, qemu_get_clock_ns(rtc_clock));
  194. break;
  195. case RTC_REG_B:
  196. if (data & REG_B_SET) {
  197. /* set mode: reset UIP mode */
  198. s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
  199. data &= ~REG_B_UIE;
  200. } else {
  201. /* if disabling set mode, update the time */
  202. if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
  203. rtc_set_time(s);
  204. }
  205. }
  206. if (((s->cmos_data[RTC_REG_B] ^ data) & (REG_B_DM | REG_B_24H)) &&
  207. !(data & REG_B_SET)) {
  208. /* If the time format has changed and not in set mode,
  209. update the registers immediately. */
  210. s->cmos_data[RTC_REG_B] = data;
  211. rtc_copy_date(s);
  212. } else {
  213. s->cmos_data[RTC_REG_B] = data;
  214. }
  215. rtc_timer_update(s, qemu_get_clock_ns(rtc_clock));
  216. break;
  217. case RTC_REG_C:
  218. case RTC_REG_D:
  219. /* cannot write to them */
  220. break;
  221. default:
  222. s->cmos_data[s->cmos_index] = data;
  223. break;
  224. }
  225. }
  226. }
  227. static inline int rtc_to_bcd(RTCState *s, int a)
  228. {
  229. if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
  230. return a;
  231. } else {
  232. return ((a / 10) << 4) | (a % 10);
  233. }
  234. }
  235. static inline int rtc_from_bcd(RTCState *s, int a)
  236. {
  237. if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
  238. return a;
  239. } else {
  240. return ((a >> 4) * 10) + (a & 0x0f);
  241. }
  242. }
  243. static void rtc_set_time(RTCState *s)
  244. {
  245. struct tm *tm = &s->current_tm;
  246. tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
  247. tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
  248. tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
  249. if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
  250. tm->tm_hour %= 12;
  251. if (s->cmos_data[RTC_HOURS] & 0x80) {
  252. tm->tm_hour += 12;
  253. }
  254. }
  255. tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
  256. tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
  257. tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
  258. tm->tm_year = rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
  259. rtc_change_mon_event(tm);
  260. }
  261. static void rtc_copy_date(RTCState *s)
  262. {
  263. const struct tm *tm = &s->current_tm;
  264. int year;
  265. s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
  266. s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
  267. if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
  268. /* 24 hour format */
  269. s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
  270. } else {
  271. /* 12 hour format */
  272. int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
  273. s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
  274. if (tm->tm_hour >= 12)
  275. s->cmos_data[RTC_HOURS] |= 0x80;
  276. }
  277. s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
  278. s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
  279. s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
  280. year = (tm->tm_year - s->base_year) % 100;
  281. if (year < 0)
  282. year += 100;
  283. s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year);
  284. }
  285. /* month is between 0 and 11. */
  286. static int get_days_in_month(int month, int year)
  287. {
  288. static const int days_tab[12] = {
  289. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  290. };
  291. int d;
  292. if ((unsigned )month >= 12)
  293. return 31;
  294. d = days_tab[month];
  295. if (month == 1) {
  296. if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
  297. d++;
  298. }
  299. return d;
  300. }
  301. /* update 'tm' to the next second */
  302. static void rtc_next_second(struct tm *tm)
  303. {
  304. int days_in_month;
  305. tm->tm_sec++;
  306. if ((unsigned)tm->tm_sec >= 60) {
  307. tm->tm_sec = 0;
  308. tm->tm_min++;
  309. if ((unsigned)tm->tm_min >= 60) {
  310. tm->tm_min = 0;
  311. tm->tm_hour++;
  312. if ((unsigned)tm->tm_hour >= 24) {
  313. tm->tm_hour = 0;
  314. /* next day */
  315. tm->tm_wday++;
  316. if ((unsigned)tm->tm_wday >= 7)
  317. tm->tm_wday = 0;
  318. days_in_month = get_days_in_month(tm->tm_mon,
  319. tm->tm_year + 1900);
  320. tm->tm_mday++;
  321. if (tm->tm_mday < 1) {
  322. tm->tm_mday = 1;
  323. } else if (tm->tm_mday > days_in_month) {
  324. tm->tm_mday = 1;
  325. tm->tm_mon++;
  326. if (tm->tm_mon >= 12) {
  327. tm->tm_mon = 0;
  328. tm->tm_year++;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. static void rtc_update_second(void *opaque)
  336. {
  337. RTCState *s = opaque;
  338. int64_t delay;
  339. /* if the oscillator is not in normal operation, we do not update */
  340. if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
  341. s->next_second_time += get_ticks_per_sec();
  342. qemu_mod_timer(s->second_timer, s->next_second_time);
  343. } else {
  344. rtc_next_second(&s->current_tm);
  345. if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
  346. /* update in progress bit */
  347. s->cmos_data[RTC_REG_A] |= REG_A_UIP;
  348. }
  349. /* should be 244 us = 8 / 32768 seconds, but currently the
  350. timers do not have the necessary resolution. */
  351. delay = (get_ticks_per_sec() * 1) / 100;
  352. if (delay < 1)
  353. delay = 1;
  354. qemu_mod_timer(s->second_timer2,
  355. s->next_second_time + delay);
  356. }
  357. }
  358. static void rtc_update_second2(void *opaque)
  359. {
  360. RTCState *s = opaque;
  361. if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
  362. rtc_copy_date(s);
  363. }
  364. /* check alarm */
  365. if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
  366. rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]) == s->current_tm.tm_sec) &&
  367. ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
  368. rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]) == s->current_tm.tm_min) &&
  369. ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
  370. rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]) == s->current_tm.tm_hour)) {
  371. s->cmos_data[RTC_REG_C] |= REG_C_AF;
  372. if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
  373. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC);
  374. qemu_irq_raise(s->irq);
  375. s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
  376. }
  377. }
  378. /* update ended interrupt */
  379. s->cmos_data[RTC_REG_C] |= REG_C_UF;
  380. if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
  381. s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
  382. qemu_irq_raise(s->irq);
  383. }
  384. /* clear update in progress bit */
  385. s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
  386. s->next_second_time += get_ticks_per_sec();
  387. qemu_mod_timer(s->second_timer, s->next_second_time);
  388. }
  389. static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
  390. {
  391. RTCState *s = opaque;
  392. int ret;
  393. if ((addr & 1) == 0) {
  394. return 0xff;
  395. } else {
  396. switch(s->cmos_index) {
  397. case RTC_SECONDS:
  398. case RTC_MINUTES:
  399. case RTC_HOURS:
  400. case RTC_DAY_OF_WEEK:
  401. case RTC_DAY_OF_MONTH:
  402. case RTC_MONTH:
  403. case RTC_YEAR:
  404. ret = s->cmos_data[s->cmos_index];
  405. break;
  406. case RTC_REG_A:
  407. ret = s->cmos_data[s->cmos_index];
  408. break;
  409. case RTC_REG_C:
  410. ret = s->cmos_data[s->cmos_index];
  411. qemu_irq_lower(s->irq);
  412. s->cmos_data[RTC_REG_C] = 0x00;
  413. #ifdef TARGET_I386
  414. if(s->irq_coalesced &&
  415. (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
  416. s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
  417. s->irq_reinject_on_ack_count++;
  418. s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
  419. apic_reset_irq_delivered();
  420. DPRINTF_C("cmos: injecting on ack\n");
  421. qemu_irq_raise(s->irq);
  422. if (apic_get_irq_delivered()) {
  423. s->irq_coalesced--;
  424. DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
  425. s->irq_coalesced);
  426. }
  427. }
  428. #endif
  429. break;
  430. default:
  431. ret = s->cmos_data[s->cmos_index];
  432. break;
  433. }
  434. CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
  435. s->cmos_index, ret);
  436. return ret;
  437. }
  438. }
  439. void rtc_set_memory(ISADevice *dev, int addr, int val)
  440. {
  441. RTCState *s = DO_UPCAST(RTCState, dev, dev);
  442. if (addr >= 0 && addr <= 127)
  443. s->cmos_data[addr] = val;
  444. }
  445. void rtc_set_date(ISADevice *dev, const struct tm *tm)
  446. {
  447. RTCState *s = DO_UPCAST(RTCState, dev, dev);
  448. s->current_tm = *tm;
  449. rtc_copy_date(s);
  450. }
  451. /* PC cmos mappings */
  452. #define REG_IBM_CENTURY_BYTE 0x32
  453. #define REG_IBM_PS2_CENTURY_BYTE 0x37
  454. static void rtc_set_date_from_host(ISADevice *dev)
  455. {
  456. RTCState *s = DO_UPCAST(RTCState, dev, dev);
  457. struct tm tm;
  458. int val;
  459. /* set the CMOS date */
  460. qemu_get_timedate(&tm, 0);
  461. rtc_set_date(dev, &tm);
  462. val = rtc_to_bcd(s, (tm.tm_year / 100) + 19);
  463. rtc_set_memory(dev, REG_IBM_CENTURY_BYTE, val);
  464. rtc_set_memory(dev, REG_IBM_PS2_CENTURY_BYTE, val);
  465. }
  466. static int rtc_post_load(void *opaque, int version_id)
  467. {
  468. #ifdef TARGET_I386
  469. RTCState *s = opaque;
  470. if (version_id >= 2) {
  471. if (s->lost_tick_policy == LOST_TICK_SLEW) {
  472. rtc_coalesced_timer_update(s);
  473. }
  474. }
  475. #endif
  476. return 0;
  477. }
  478. static const VMStateDescription vmstate_rtc = {
  479. .name = "mc146818rtc",
  480. .version_id = 2,
  481. .minimum_version_id = 1,
  482. .minimum_version_id_old = 1,
  483. .post_load = rtc_post_load,
  484. .fields = (VMStateField []) {
  485. VMSTATE_BUFFER(cmos_data, RTCState),
  486. VMSTATE_UINT8(cmos_index, RTCState),
  487. VMSTATE_INT32(current_tm.tm_sec, RTCState),
  488. VMSTATE_INT32(current_tm.tm_min, RTCState),
  489. VMSTATE_INT32(current_tm.tm_hour, RTCState),
  490. VMSTATE_INT32(current_tm.tm_wday, RTCState),
  491. VMSTATE_INT32(current_tm.tm_mday, RTCState),
  492. VMSTATE_INT32(current_tm.tm_mon, RTCState),
  493. VMSTATE_INT32(current_tm.tm_year, RTCState),
  494. VMSTATE_TIMER(periodic_timer, RTCState),
  495. VMSTATE_INT64(next_periodic_time, RTCState),
  496. VMSTATE_INT64(next_second_time, RTCState),
  497. VMSTATE_TIMER(second_timer, RTCState),
  498. VMSTATE_TIMER(second_timer2, RTCState),
  499. VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
  500. VMSTATE_UINT32_V(period, RTCState, 2),
  501. VMSTATE_END_OF_LIST()
  502. }
  503. };
  504. static void rtc_notify_clock_reset(Notifier *notifier, void *data)
  505. {
  506. RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
  507. int64_t now = *(int64_t *)data;
  508. rtc_set_date_from_host(&s->dev);
  509. s->next_second_time = now + (get_ticks_per_sec() * 99) / 100;
  510. qemu_mod_timer(s->second_timer2, s->next_second_time);
  511. rtc_timer_update(s, now);
  512. #ifdef TARGET_I386
  513. if (s->lost_tick_policy == LOST_TICK_SLEW) {
  514. rtc_coalesced_timer_update(s);
  515. }
  516. #endif
  517. }
  518. /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
  519. BIOS will read it and start S3 resume at POST Entry */
  520. static void rtc_notify_suspend(Notifier *notifier, void *data)
  521. {
  522. RTCState *s = container_of(notifier, RTCState, suspend_notifier);
  523. rtc_set_memory(&s->dev, 0xF, 0xFE);
  524. }
  525. static void rtc_reset(void *opaque)
  526. {
  527. RTCState *s = opaque;
  528. s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
  529. s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
  530. qemu_irq_lower(s->irq);
  531. #ifdef TARGET_I386
  532. if (s->lost_tick_policy == LOST_TICK_SLEW) {
  533. s->irq_coalesced = 0;
  534. }
  535. #endif
  536. }
  537. static const MemoryRegionPortio cmos_portio[] = {
  538. {0, 2, 1, .read = cmos_ioport_read, .write = cmos_ioport_write },
  539. PORTIO_END_OF_LIST(),
  540. };
  541. static const MemoryRegionOps cmos_ops = {
  542. .old_portio = cmos_portio
  543. };
  544. static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
  545. const char *name, Error **errp)
  546. {
  547. ISADevice *isa = ISA_DEVICE(obj);
  548. RTCState *s = DO_UPCAST(RTCState, dev, isa);
  549. visit_start_struct(v, NULL, "struct tm", name, 0, errp);
  550. visit_type_int32(v, &s->current_tm.tm_year, "tm_year", errp);
  551. visit_type_int32(v, &s->current_tm.tm_mon, "tm_mon", errp);
  552. visit_type_int32(v, &s->current_tm.tm_mday, "tm_mday", errp);
  553. visit_type_int32(v, &s->current_tm.tm_hour, "tm_hour", errp);
  554. visit_type_int32(v, &s->current_tm.tm_min, "tm_min", errp);
  555. visit_type_int32(v, &s->current_tm.tm_sec, "tm_sec", errp);
  556. visit_end_struct(v, errp);
  557. }
  558. static int rtc_initfn(ISADevice *dev)
  559. {
  560. RTCState *s = DO_UPCAST(RTCState, dev, dev);
  561. int base = 0x70;
  562. s->cmos_data[RTC_REG_A] = 0x26;
  563. s->cmos_data[RTC_REG_B] = 0x02;
  564. s->cmos_data[RTC_REG_C] = 0x00;
  565. s->cmos_data[RTC_REG_D] = 0x80;
  566. rtc_set_date_from_host(dev);
  567. #ifdef TARGET_I386
  568. switch (s->lost_tick_policy) {
  569. case LOST_TICK_SLEW:
  570. s->coalesced_timer =
  571. qemu_new_timer_ns(rtc_clock, rtc_coalesced_timer, s);
  572. break;
  573. case LOST_TICK_DISCARD:
  574. break;
  575. default:
  576. return -EINVAL;
  577. }
  578. #endif
  579. s->periodic_timer = qemu_new_timer_ns(rtc_clock, rtc_periodic_timer, s);
  580. s->second_timer = qemu_new_timer_ns(rtc_clock, rtc_update_second, s);
  581. s->second_timer2 = qemu_new_timer_ns(rtc_clock, rtc_update_second2, s);
  582. s->clock_reset_notifier.notify = rtc_notify_clock_reset;
  583. qemu_register_clock_reset_notifier(rtc_clock, &s->clock_reset_notifier);
  584. s->suspend_notifier.notify = rtc_notify_suspend;
  585. qemu_register_suspend_notifier(&s->suspend_notifier);
  586. s->next_second_time =
  587. qemu_get_clock_ns(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
  588. qemu_mod_timer(s->second_timer2, s->next_second_time);
  589. memory_region_init_io(&s->io, &cmos_ops, s, "rtc", 2);
  590. isa_register_ioport(dev, &s->io, base);
  591. qdev_set_legacy_instance_id(&dev->qdev, base, 2);
  592. qemu_register_reset(rtc_reset, s);
  593. object_property_add(OBJECT(s), "date", "struct tm",
  594. rtc_get_date, NULL, NULL, s, NULL);
  595. return 0;
  596. }
  597. ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
  598. {
  599. ISADevice *dev;
  600. RTCState *s;
  601. dev = isa_create(bus, "mc146818rtc");
  602. s = DO_UPCAST(RTCState, dev, dev);
  603. qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
  604. qdev_init_nofail(&dev->qdev);
  605. if (intercept_irq) {
  606. s->irq = intercept_irq;
  607. } else {
  608. isa_init_irq(dev, &s->irq, RTC_ISA_IRQ);
  609. }
  610. return dev;
  611. }
  612. static Property mc146818rtc_properties[] = {
  613. DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
  614. DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
  615. lost_tick_policy, LOST_TICK_DISCARD),
  616. DEFINE_PROP_END_OF_LIST(),
  617. };
  618. static void rtc_class_initfn(ObjectClass *klass, void *data)
  619. {
  620. DeviceClass *dc = DEVICE_CLASS(klass);
  621. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  622. ic->init = rtc_initfn;
  623. dc->no_user = 1;
  624. dc->vmsd = &vmstate_rtc;
  625. dc->props = mc146818rtc_properties;
  626. }
  627. static TypeInfo mc146818rtc_info = {
  628. .name = "mc146818rtc",
  629. .parent = TYPE_ISA_DEVICE,
  630. .instance_size = sizeof(RTCState),
  631. .class_init = rtc_class_initfn,
  632. };
  633. static void mc146818rtc_register_types(void)
  634. {
  635. type_register_static(&mc146818rtc_info);
  636. }
  637. type_init(mc146818rtc_register_types)