npcm7xx_timer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Nuvoton NPCM7xx Timer Controller
  3. *
  4. * Copyright 2020 Google LLC
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "hw/irq.h"
  18. #include "hw/qdev-clock.h"
  19. #include "hw/qdev-properties.h"
  20. #include "hw/timer/npcm7xx_timer.h"
  21. #include "migration/vmstate.h"
  22. #include "qemu/bitops.h"
  23. #include "qemu/error-report.h"
  24. #include "qemu/log.h"
  25. #include "qemu/module.h"
  26. #include "qemu/timer.h"
  27. #include "qemu/units.h"
  28. #include "trace.h"
  29. /* 32-bit register indices. */
  30. enum NPCM7xxTimerRegisters {
  31. NPCM7XX_TIMER_TCSR0,
  32. NPCM7XX_TIMER_TCSR1,
  33. NPCM7XX_TIMER_TICR0,
  34. NPCM7XX_TIMER_TICR1,
  35. NPCM7XX_TIMER_TDR0,
  36. NPCM7XX_TIMER_TDR1,
  37. NPCM7XX_TIMER_TISR,
  38. NPCM7XX_TIMER_WTCR,
  39. NPCM7XX_TIMER_TCSR2,
  40. NPCM7XX_TIMER_TCSR3,
  41. NPCM7XX_TIMER_TICR2,
  42. NPCM7XX_TIMER_TICR3,
  43. NPCM7XX_TIMER_TDR2,
  44. NPCM7XX_TIMER_TDR3,
  45. NPCM7XX_TIMER_TCSR4 = 0x0040 / sizeof(uint32_t),
  46. NPCM7XX_TIMER_TICR4 = 0x0048 / sizeof(uint32_t),
  47. NPCM7XX_TIMER_TDR4 = 0x0050 / sizeof(uint32_t),
  48. NPCM7XX_TIMER_REGS_END,
  49. };
  50. /* Register field definitions. */
  51. #define NPCM7XX_TCSR_CEN BIT(30)
  52. #define NPCM7XX_TCSR_IE BIT(29)
  53. #define NPCM7XX_TCSR_PERIODIC BIT(27)
  54. #define NPCM7XX_TCSR_CRST BIT(26)
  55. #define NPCM7XX_TCSR_CACT BIT(25)
  56. #define NPCM7XX_TCSR_RSVD 0x01ffff00
  57. #define NPCM7XX_TCSR_PRESCALE_START 0
  58. #define NPCM7XX_TCSR_PRESCALE_LEN 8
  59. #define NPCM7XX_WTCR_WTCLK(rv) extract32(rv, 10, 2)
  60. #define NPCM7XX_WTCR_FREEZE_EN BIT(9)
  61. #define NPCM7XX_WTCR_WTE BIT(7)
  62. #define NPCM7XX_WTCR_WTIE BIT(6)
  63. #define NPCM7XX_WTCR_WTIS(rv) extract32(rv, 4, 2)
  64. #define NPCM7XX_WTCR_WTIF BIT(3)
  65. #define NPCM7XX_WTCR_WTRF BIT(2)
  66. #define NPCM7XX_WTCR_WTRE BIT(1)
  67. #define NPCM7XX_WTCR_WTR BIT(0)
  68. /*
  69. * The number of clock cycles between interrupt and reset in watchdog, used
  70. * by the software to handle the interrupt before system is reset.
  71. */
  72. #define NPCM7XX_WATCHDOG_INTERRUPT_TO_RESET_CYCLES 1024
  73. /* Start or resume the timer. */
  74. static void npcm7xx_timer_start(NPCM7xxBaseTimer *t)
  75. {
  76. int64_t now;
  77. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  78. t->expires_ns = now + t->remaining_ns;
  79. timer_mod(&t->qtimer, t->expires_ns);
  80. }
  81. /* Stop counting. Record the time remaining so we can continue later. */
  82. static void npcm7xx_timer_pause(NPCM7xxBaseTimer *t)
  83. {
  84. int64_t now;
  85. timer_del(&t->qtimer);
  86. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  87. t->remaining_ns = t->expires_ns - now;
  88. }
  89. /* Delete the timer and reset it to default state. */
  90. static void npcm7xx_timer_clear(NPCM7xxBaseTimer *t)
  91. {
  92. timer_del(&t->qtimer);
  93. t->expires_ns = 0;
  94. t->remaining_ns = 0;
  95. }
  96. /*
  97. * Returns the index of timer in the tc->timer array. This can be used to
  98. * locate the registers that belong to this timer.
  99. */
  100. static int npcm7xx_timer_index(NPCM7xxTimerCtrlState *tc, NPCM7xxTimer *timer)
  101. {
  102. int index = timer - tc->timer;
  103. g_assert(index >= 0 && index < NPCM7XX_TIMERS_PER_CTRL);
  104. return index;
  105. }
  106. /* Return the value by which to divide the reference clock rate. */
  107. static uint32_t npcm7xx_tcsr_prescaler(uint32_t tcsr)
  108. {
  109. return extract32(tcsr, NPCM7XX_TCSR_PRESCALE_START,
  110. NPCM7XX_TCSR_PRESCALE_LEN) + 1;
  111. }
  112. /* Convert a timer cycle count to a time interval in nanoseconds. */
  113. static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer *t, uint32_t count)
  114. {
  115. int64_t ticks = count;
  116. ticks *= npcm7xx_tcsr_prescaler(t->tcsr);
  117. return clock_ticks_to_ns(t->ctrl->clock, ticks);
  118. }
  119. /* Convert a time interval in nanoseconds to a timer cycle count. */
  120. static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer *t, int64_t ns)
  121. {
  122. return clock_ns_to_ticks(t->ctrl->clock, ns) /
  123. npcm7xx_tcsr_prescaler(t->tcsr);
  124. }
  125. static uint32_t npcm7xx_watchdog_timer_prescaler(const NPCM7xxWatchdogTimer *t)
  126. {
  127. switch (NPCM7XX_WTCR_WTCLK(t->wtcr)) {
  128. case 0:
  129. return 1;
  130. case 1:
  131. return 256;
  132. case 2:
  133. return 2048;
  134. case 3:
  135. return 65536;
  136. default:
  137. g_assert_not_reached();
  138. }
  139. }
  140. static void npcm7xx_watchdog_timer_reset_cycles(NPCM7xxWatchdogTimer *t,
  141. int64_t cycles)
  142. {
  143. int64_t ticks = cycles * npcm7xx_watchdog_timer_prescaler(t);
  144. int64_t ns = clock_ticks_to_ns(t->ctrl->clock, ticks);
  145. /*
  146. * The reset function always clears the current timer. The caller of the
  147. * this needs to decide whether to start the watchdog timer based on
  148. * specific flag in WTCR.
  149. */
  150. npcm7xx_timer_clear(&t->base_timer);
  151. t->base_timer.remaining_ns = ns;
  152. }
  153. static void npcm7xx_watchdog_timer_reset(NPCM7xxWatchdogTimer *t)
  154. {
  155. int64_t cycles = 1;
  156. uint32_t s = NPCM7XX_WTCR_WTIS(t->wtcr);
  157. g_assert(s <= 3);
  158. cycles <<= NPCM7XX_WATCHDOG_BASETIME_SHIFT;
  159. cycles <<= 2 * s;
  160. npcm7xx_watchdog_timer_reset_cycles(t, cycles);
  161. }
  162. /*
  163. * Raise the interrupt line if there's a pending interrupt and interrupts are
  164. * enabled for this timer. If not, lower it.
  165. */
  166. static void npcm7xx_timer_check_interrupt(NPCM7xxTimer *t)
  167. {
  168. NPCM7xxTimerCtrlState *tc = t->ctrl;
  169. int index = npcm7xx_timer_index(tc, t);
  170. bool pending = (t->tcsr & NPCM7XX_TCSR_IE) && (tc->tisr & BIT(index));
  171. qemu_set_irq(t->irq, pending);
  172. trace_npcm7xx_timer_irq(DEVICE(tc)->canonical_path, index, pending);
  173. }
  174. /*
  175. * Called when the counter reaches zero. Sets the interrupt flag, and either
  176. * restarts or disables the timer.
  177. */
  178. static void npcm7xx_timer_reached_zero(NPCM7xxTimer *t)
  179. {
  180. NPCM7xxTimerCtrlState *tc = t->ctrl;
  181. int index = npcm7xx_timer_index(tc, t);
  182. tc->tisr |= BIT(index);
  183. if (t->tcsr & NPCM7XX_TCSR_PERIODIC) {
  184. t->base_timer.remaining_ns = npcm7xx_timer_count_to_ns(t, t->ticr);
  185. if (t->tcsr & NPCM7XX_TCSR_CEN) {
  186. npcm7xx_timer_start(&t->base_timer);
  187. }
  188. } else {
  189. t->tcsr &= ~(NPCM7XX_TCSR_CEN | NPCM7XX_TCSR_CACT);
  190. }
  191. npcm7xx_timer_check_interrupt(t);
  192. }
  193. /*
  194. * Restart the timer from its initial value. If the timer was enabled and stays
  195. * enabled, adjust the QEMU timer according to the new count. If the timer is
  196. * transitioning from disabled to enabled, the caller is expected to start the
  197. * timer later.
  198. */
  199. static void npcm7xx_timer_restart(NPCM7xxTimer *t, uint32_t old_tcsr)
  200. {
  201. t->base_timer.remaining_ns = npcm7xx_timer_count_to_ns(t, t->ticr);
  202. if (old_tcsr & t->tcsr & NPCM7XX_TCSR_CEN) {
  203. npcm7xx_timer_start(&t->base_timer);
  204. }
  205. }
  206. /* Register read and write handlers */
  207. static uint32_t npcm7xx_timer_read_tdr(NPCM7xxTimer *t)
  208. {
  209. if (t->tcsr & NPCM7XX_TCSR_CEN) {
  210. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  211. return npcm7xx_timer_ns_to_count(t, t->base_timer.expires_ns - now);
  212. }
  213. return npcm7xx_timer_ns_to_count(t, t->base_timer.remaining_ns);
  214. }
  215. static void npcm7xx_timer_write_tcsr(NPCM7xxTimer *t, uint32_t new_tcsr)
  216. {
  217. uint32_t old_tcsr = t->tcsr;
  218. uint32_t tdr;
  219. if (new_tcsr & NPCM7XX_TCSR_RSVD) {
  220. qemu_log_mask(LOG_GUEST_ERROR, "%s: reserved bits in 0x%08x ignored\n",
  221. __func__, new_tcsr);
  222. new_tcsr &= ~NPCM7XX_TCSR_RSVD;
  223. }
  224. if (new_tcsr & NPCM7XX_TCSR_CACT) {
  225. qemu_log_mask(LOG_GUEST_ERROR, "%s: read-only bits in 0x%08x ignored\n",
  226. __func__, new_tcsr);
  227. new_tcsr &= ~NPCM7XX_TCSR_CACT;
  228. }
  229. if ((new_tcsr & NPCM7XX_TCSR_CRST) && (new_tcsr & NPCM7XX_TCSR_CEN)) {
  230. qemu_log_mask(LOG_GUEST_ERROR,
  231. "%s: both CRST and CEN set; ignoring CEN.\n",
  232. __func__);
  233. new_tcsr &= ~NPCM7XX_TCSR_CEN;
  234. }
  235. /* Calculate the value of TDR before potentially changing the prescaler. */
  236. tdr = npcm7xx_timer_read_tdr(t);
  237. t->tcsr = (t->tcsr & NPCM7XX_TCSR_CACT) | new_tcsr;
  238. if (npcm7xx_tcsr_prescaler(old_tcsr) != npcm7xx_tcsr_prescaler(new_tcsr)) {
  239. /* Recalculate time remaining based on the current TDR value. */
  240. t->base_timer.remaining_ns = npcm7xx_timer_count_to_ns(t, tdr);
  241. if (old_tcsr & t->tcsr & NPCM7XX_TCSR_CEN) {
  242. npcm7xx_timer_start(&t->base_timer);
  243. }
  244. }
  245. if ((old_tcsr ^ new_tcsr) & NPCM7XX_TCSR_IE) {
  246. npcm7xx_timer_check_interrupt(t);
  247. }
  248. if (new_tcsr & NPCM7XX_TCSR_CRST) {
  249. npcm7xx_timer_restart(t, old_tcsr);
  250. t->tcsr &= ~NPCM7XX_TCSR_CRST;
  251. }
  252. if ((old_tcsr ^ new_tcsr) & NPCM7XX_TCSR_CEN) {
  253. if (new_tcsr & NPCM7XX_TCSR_CEN) {
  254. t->tcsr |= NPCM7XX_TCSR_CACT;
  255. npcm7xx_timer_start(&t->base_timer);
  256. } else {
  257. t->tcsr &= ~NPCM7XX_TCSR_CACT;
  258. npcm7xx_timer_pause(&t->base_timer);
  259. if (t->base_timer.remaining_ns <= 0) {
  260. npcm7xx_timer_reached_zero(t);
  261. }
  262. }
  263. }
  264. }
  265. static void npcm7xx_timer_write_ticr(NPCM7xxTimer *t, uint32_t new_ticr)
  266. {
  267. t->ticr = new_ticr;
  268. npcm7xx_timer_restart(t, t->tcsr);
  269. }
  270. static void npcm7xx_timer_write_tisr(NPCM7xxTimerCtrlState *s, uint32_t value)
  271. {
  272. int i;
  273. s->tisr &= ~value;
  274. for (i = 0; i < ARRAY_SIZE(s->timer); i++) {
  275. if (value & (1U << i)) {
  276. npcm7xx_timer_check_interrupt(&s->timer[i]);
  277. }
  278. }
  279. }
  280. static void npcm7xx_timer_write_wtcr(NPCM7xxWatchdogTimer *t, uint32_t new_wtcr)
  281. {
  282. uint32_t old_wtcr = t->wtcr;
  283. /*
  284. * WTIF and WTRF are cleared by writing 1. Writing 0 makes these bits
  285. * unchanged.
  286. */
  287. if (new_wtcr & NPCM7XX_WTCR_WTIF) {
  288. new_wtcr &= ~NPCM7XX_WTCR_WTIF;
  289. } else if (old_wtcr & NPCM7XX_WTCR_WTIF) {
  290. new_wtcr |= NPCM7XX_WTCR_WTIF;
  291. }
  292. if (new_wtcr & NPCM7XX_WTCR_WTRF) {
  293. new_wtcr &= ~NPCM7XX_WTCR_WTRF;
  294. } else if (old_wtcr & NPCM7XX_WTCR_WTRF) {
  295. new_wtcr |= NPCM7XX_WTCR_WTRF;
  296. }
  297. t->wtcr = new_wtcr;
  298. if (new_wtcr & NPCM7XX_WTCR_WTR) {
  299. t->wtcr &= ~NPCM7XX_WTCR_WTR;
  300. npcm7xx_watchdog_timer_reset(t);
  301. if (new_wtcr & NPCM7XX_WTCR_WTE) {
  302. npcm7xx_timer_start(&t->base_timer);
  303. }
  304. } else if ((old_wtcr ^ new_wtcr) & NPCM7XX_WTCR_WTE) {
  305. if (new_wtcr & NPCM7XX_WTCR_WTE) {
  306. npcm7xx_timer_start(&t->base_timer);
  307. } else {
  308. npcm7xx_timer_pause(&t->base_timer);
  309. }
  310. }
  311. }
  312. static hwaddr npcm7xx_tcsr_index(hwaddr reg)
  313. {
  314. switch (reg) {
  315. case NPCM7XX_TIMER_TCSR0:
  316. return 0;
  317. case NPCM7XX_TIMER_TCSR1:
  318. return 1;
  319. case NPCM7XX_TIMER_TCSR2:
  320. return 2;
  321. case NPCM7XX_TIMER_TCSR3:
  322. return 3;
  323. case NPCM7XX_TIMER_TCSR4:
  324. return 4;
  325. default:
  326. g_assert_not_reached();
  327. }
  328. }
  329. static hwaddr npcm7xx_ticr_index(hwaddr reg)
  330. {
  331. switch (reg) {
  332. case NPCM7XX_TIMER_TICR0:
  333. return 0;
  334. case NPCM7XX_TIMER_TICR1:
  335. return 1;
  336. case NPCM7XX_TIMER_TICR2:
  337. return 2;
  338. case NPCM7XX_TIMER_TICR3:
  339. return 3;
  340. case NPCM7XX_TIMER_TICR4:
  341. return 4;
  342. default:
  343. g_assert_not_reached();
  344. }
  345. }
  346. static hwaddr npcm7xx_tdr_index(hwaddr reg)
  347. {
  348. switch (reg) {
  349. case NPCM7XX_TIMER_TDR0:
  350. return 0;
  351. case NPCM7XX_TIMER_TDR1:
  352. return 1;
  353. case NPCM7XX_TIMER_TDR2:
  354. return 2;
  355. case NPCM7XX_TIMER_TDR3:
  356. return 3;
  357. case NPCM7XX_TIMER_TDR4:
  358. return 4;
  359. default:
  360. g_assert_not_reached();
  361. }
  362. }
  363. static uint64_t npcm7xx_timer_read(void *opaque, hwaddr offset, unsigned size)
  364. {
  365. NPCM7xxTimerCtrlState *s = opaque;
  366. uint64_t value = 0;
  367. hwaddr reg;
  368. reg = offset / sizeof(uint32_t);
  369. switch (reg) {
  370. case NPCM7XX_TIMER_TCSR0:
  371. case NPCM7XX_TIMER_TCSR1:
  372. case NPCM7XX_TIMER_TCSR2:
  373. case NPCM7XX_TIMER_TCSR3:
  374. case NPCM7XX_TIMER_TCSR4:
  375. value = s->timer[npcm7xx_tcsr_index(reg)].tcsr;
  376. break;
  377. case NPCM7XX_TIMER_TICR0:
  378. case NPCM7XX_TIMER_TICR1:
  379. case NPCM7XX_TIMER_TICR2:
  380. case NPCM7XX_TIMER_TICR3:
  381. case NPCM7XX_TIMER_TICR4:
  382. value = s->timer[npcm7xx_ticr_index(reg)].ticr;
  383. break;
  384. case NPCM7XX_TIMER_TDR0:
  385. case NPCM7XX_TIMER_TDR1:
  386. case NPCM7XX_TIMER_TDR2:
  387. case NPCM7XX_TIMER_TDR3:
  388. case NPCM7XX_TIMER_TDR4:
  389. value = npcm7xx_timer_read_tdr(&s->timer[npcm7xx_tdr_index(reg)]);
  390. break;
  391. case NPCM7XX_TIMER_TISR:
  392. value = s->tisr;
  393. break;
  394. case NPCM7XX_TIMER_WTCR:
  395. value = s->watchdog_timer.wtcr;
  396. break;
  397. default:
  398. qemu_log_mask(LOG_GUEST_ERROR,
  399. "%s: invalid offset 0x%04" HWADDR_PRIx "\n",
  400. __func__, offset);
  401. break;
  402. }
  403. trace_npcm7xx_timer_read(DEVICE(s)->canonical_path, offset, value);
  404. return value;
  405. }
  406. static void npcm7xx_timer_write(void *opaque, hwaddr offset,
  407. uint64_t v, unsigned size)
  408. {
  409. uint32_t reg = offset / sizeof(uint32_t);
  410. NPCM7xxTimerCtrlState *s = opaque;
  411. uint32_t value = v;
  412. trace_npcm7xx_timer_write(DEVICE(s)->canonical_path, offset, value);
  413. switch (reg) {
  414. case NPCM7XX_TIMER_TCSR0:
  415. case NPCM7XX_TIMER_TCSR1:
  416. case NPCM7XX_TIMER_TCSR2:
  417. case NPCM7XX_TIMER_TCSR3:
  418. case NPCM7XX_TIMER_TCSR4:
  419. npcm7xx_timer_write_tcsr(&s->timer[npcm7xx_tcsr_index(reg)], value);
  420. return;
  421. case NPCM7XX_TIMER_TICR0:
  422. case NPCM7XX_TIMER_TICR1:
  423. case NPCM7XX_TIMER_TICR2:
  424. case NPCM7XX_TIMER_TICR3:
  425. case NPCM7XX_TIMER_TICR4:
  426. npcm7xx_timer_write_ticr(&s->timer[npcm7xx_ticr_index(reg)], value);
  427. return;
  428. case NPCM7XX_TIMER_TDR0:
  429. case NPCM7XX_TIMER_TDR1:
  430. case NPCM7XX_TIMER_TDR2:
  431. case NPCM7XX_TIMER_TDR3:
  432. case NPCM7XX_TIMER_TDR4:
  433. qemu_log_mask(LOG_GUEST_ERROR,
  434. "%s: register @ 0x%04" HWADDR_PRIx " is read-only\n",
  435. __func__, offset);
  436. return;
  437. case NPCM7XX_TIMER_TISR:
  438. npcm7xx_timer_write_tisr(s, value);
  439. return;
  440. case NPCM7XX_TIMER_WTCR:
  441. npcm7xx_timer_write_wtcr(&s->watchdog_timer, value);
  442. return;
  443. }
  444. qemu_log_mask(LOG_GUEST_ERROR,
  445. "%s: invalid offset 0x%04" HWADDR_PRIx "\n",
  446. __func__, offset);
  447. }
  448. static const struct MemoryRegionOps npcm7xx_timer_ops = {
  449. .read = npcm7xx_timer_read,
  450. .write = npcm7xx_timer_write,
  451. .endianness = DEVICE_LITTLE_ENDIAN,
  452. .valid = {
  453. .min_access_size = 4,
  454. .max_access_size = 4,
  455. .unaligned = false,
  456. },
  457. };
  458. /* Called when the QEMU timer expires. */
  459. static void npcm7xx_timer_expired(void *opaque)
  460. {
  461. NPCM7xxTimer *t = opaque;
  462. if (t->tcsr & NPCM7XX_TCSR_CEN) {
  463. npcm7xx_timer_reached_zero(t);
  464. }
  465. }
  466. static void npcm7xx_timer_enter_reset(Object *obj, ResetType type)
  467. {
  468. NPCM7xxTimerCtrlState *s = NPCM7XX_TIMER(obj);
  469. int i;
  470. for (i = 0; i < NPCM7XX_TIMERS_PER_CTRL; i++) {
  471. NPCM7xxTimer *t = &s->timer[i];
  472. npcm7xx_timer_clear(&t->base_timer);
  473. t->tcsr = 0x00000005;
  474. t->ticr = 0x00000000;
  475. }
  476. s->tisr = 0x00000000;
  477. /*
  478. * Set WTCLK to 1(default) and reset all flags except WTRF.
  479. * WTRF is not reset during a core domain reset.
  480. */
  481. s->watchdog_timer.wtcr = 0x00000400 | (s->watchdog_timer.wtcr &
  482. NPCM7XX_WTCR_WTRF);
  483. }
  484. static void npcm7xx_watchdog_timer_expired(void *opaque)
  485. {
  486. NPCM7xxWatchdogTimer *t = opaque;
  487. if (t->wtcr & NPCM7XX_WTCR_WTE) {
  488. if (t->wtcr & NPCM7XX_WTCR_WTIF) {
  489. if (t->wtcr & NPCM7XX_WTCR_WTRE) {
  490. t->wtcr |= NPCM7XX_WTCR_WTRF;
  491. /* send reset signal to CLK module*/
  492. qemu_irq_raise(t->reset_signal);
  493. }
  494. } else {
  495. t->wtcr |= NPCM7XX_WTCR_WTIF;
  496. if (t->wtcr & NPCM7XX_WTCR_WTIE) {
  497. /* send interrupt */
  498. qemu_irq_raise(t->irq);
  499. }
  500. npcm7xx_watchdog_timer_reset_cycles(t,
  501. NPCM7XX_WATCHDOG_INTERRUPT_TO_RESET_CYCLES);
  502. npcm7xx_timer_start(&t->base_timer);
  503. }
  504. }
  505. }
  506. static void npcm7xx_timer_hold_reset(Object *obj)
  507. {
  508. NPCM7xxTimerCtrlState *s = NPCM7XX_TIMER(obj);
  509. int i;
  510. for (i = 0; i < NPCM7XX_TIMERS_PER_CTRL; i++) {
  511. qemu_irq_lower(s->timer[i].irq);
  512. }
  513. qemu_irq_lower(s->watchdog_timer.irq);
  514. }
  515. static void npcm7xx_timer_init(Object *obj)
  516. {
  517. NPCM7xxTimerCtrlState *s = NPCM7XX_TIMER(obj);
  518. DeviceState *dev = DEVICE(obj);
  519. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  520. int i;
  521. NPCM7xxWatchdogTimer *w;
  522. for (i = 0; i < NPCM7XX_TIMERS_PER_CTRL; i++) {
  523. NPCM7xxTimer *t = &s->timer[i];
  524. t->ctrl = s;
  525. timer_init_ns(&t->base_timer.qtimer, QEMU_CLOCK_VIRTUAL,
  526. npcm7xx_timer_expired, t);
  527. sysbus_init_irq(sbd, &t->irq);
  528. }
  529. w = &s->watchdog_timer;
  530. w->ctrl = s;
  531. timer_init_ns(&w->base_timer.qtimer, QEMU_CLOCK_VIRTUAL,
  532. npcm7xx_watchdog_timer_expired, w);
  533. sysbus_init_irq(sbd, &w->irq);
  534. memory_region_init_io(&s->iomem, obj, &npcm7xx_timer_ops, s,
  535. TYPE_NPCM7XX_TIMER, 4 * KiB);
  536. sysbus_init_mmio(sbd, &s->iomem);
  537. qdev_init_gpio_out_named(dev, &w->reset_signal,
  538. NPCM7XX_WATCHDOG_RESET_GPIO_OUT, 1);
  539. s->clock = qdev_init_clock_in(dev, "clock", NULL, NULL, 0);
  540. }
  541. static const VMStateDescription vmstate_npcm7xx_base_timer = {
  542. .name = "npcm7xx-base-timer",
  543. .version_id = 0,
  544. .minimum_version_id = 0,
  545. .fields = (VMStateField[]) {
  546. VMSTATE_TIMER(qtimer, NPCM7xxBaseTimer),
  547. VMSTATE_INT64(expires_ns, NPCM7xxBaseTimer),
  548. VMSTATE_INT64(remaining_ns, NPCM7xxBaseTimer),
  549. VMSTATE_END_OF_LIST(),
  550. },
  551. };
  552. static const VMStateDescription vmstate_npcm7xx_timer = {
  553. .name = "npcm7xx-timer",
  554. .version_id = 1,
  555. .minimum_version_id = 1,
  556. .fields = (VMStateField[]) {
  557. VMSTATE_STRUCT(base_timer, NPCM7xxTimer,
  558. 0, vmstate_npcm7xx_base_timer,
  559. NPCM7xxBaseTimer),
  560. VMSTATE_UINT32(tcsr, NPCM7xxTimer),
  561. VMSTATE_UINT32(ticr, NPCM7xxTimer),
  562. VMSTATE_END_OF_LIST(),
  563. },
  564. };
  565. static const VMStateDescription vmstate_npcm7xx_watchdog_timer = {
  566. .name = "npcm7xx-watchdog-timer",
  567. .version_id = 0,
  568. .minimum_version_id = 0,
  569. .fields = (VMStateField[]) {
  570. VMSTATE_STRUCT(base_timer, NPCM7xxWatchdogTimer,
  571. 0, vmstate_npcm7xx_base_timer,
  572. NPCM7xxBaseTimer),
  573. VMSTATE_UINT32(wtcr, NPCM7xxWatchdogTimer),
  574. VMSTATE_END_OF_LIST(),
  575. },
  576. };
  577. static const VMStateDescription vmstate_npcm7xx_timer_ctrl = {
  578. .name = "npcm7xx-timer-ctrl",
  579. .version_id = 2,
  580. .minimum_version_id = 2,
  581. .fields = (VMStateField[]) {
  582. VMSTATE_UINT32(tisr, NPCM7xxTimerCtrlState),
  583. VMSTATE_CLOCK(clock, NPCM7xxTimerCtrlState),
  584. VMSTATE_STRUCT_ARRAY(timer, NPCM7xxTimerCtrlState,
  585. NPCM7XX_TIMERS_PER_CTRL, 0, vmstate_npcm7xx_timer,
  586. NPCM7xxTimer),
  587. VMSTATE_STRUCT(watchdog_timer, NPCM7xxTimerCtrlState,
  588. 0, vmstate_npcm7xx_watchdog_timer,
  589. NPCM7xxWatchdogTimer),
  590. VMSTATE_END_OF_LIST(),
  591. },
  592. };
  593. static void npcm7xx_timer_class_init(ObjectClass *klass, void *data)
  594. {
  595. ResettableClass *rc = RESETTABLE_CLASS(klass);
  596. DeviceClass *dc = DEVICE_CLASS(klass);
  597. QEMU_BUILD_BUG_ON(NPCM7XX_TIMER_REGS_END > NPCM7XX_TIMER_NR_REGS);
  598. dc->desc = "NPCM7xx Timer Controller";
  599. dc->vmsd = &vmstate_npcm7xx_timer_ctrl;
  600. rc->phases.enter = npcm7xx_timer_enter_reset;
  601. rc->phases.hold = npcm7xx_timer_hold_reset;
  602. }
  603. static const TypeInfo npcm7xx_timer_info = {
  604. .name = TYPE_NPCM7XX_TIMER,
  605. .parent = TYPE_SYS_BUS_DEVICE,
  606. .instance_size = sizeof(NPCM7xxTimerCtrlState),
  607. .class_init = npcm7xx_timer_class_init,
  608. .instance_init = npcm7xx_timer_init,
  609. };
  610. static void npcm7xx_timer_register_type(void)
  611. {
  612. type_register_static(&npcm7xx_timer_info);
  613. }
  614. type_init(npcm7xx_timer_register_type);