2
0

npcm7xx_timer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/misc/npcm7xx_clk.h"
  19. #include "hw/timer/npcm7xx_timer.h"
  20. #include "migration/vmstate.h"
  21. #include "qemu/bitops.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "qemu/timer.h"
  26. #include "qemu/units.h"
  27. #include "trace.h"
  28. /* 32-bit register indices. */
  29. enum NPCM7xxTimerRegisters {
  30. NPCM7XX_TIMER_TCSR0,
  31. NPCM7XX_TIMER_TCSR1,
  32. NPCM7XX_TIMER_TICR0,
  33. NPCM7XX_TIMER_TICR1,
  34. NPCM7XX_TIMER_TDR0,
  35. NPCM7XX_TIMER_TDR1,
  36. NPCM7XX_TIMER_TISR,
  37. NPCM7XX_TIMER_WTCR,
  38. NPCM7XX_TIMER_TCSR2,
  39. NPCM7XX_TIMER_TCSR3,
  40. NPCM7XX_TIMER_TICR2,
  41. NPCM7XX_TIMER_TICR3,
  42. NPCM7XX_TIMER_TDR2,
  43. NPCM7XX_TIMER_TDR3,
  44. NPCM7XX_TIMER_TCSR4 = 0x0040 / sizeof(uint32_t),
  45. NPCM7XX_TIMER_TICR4 = 0x0048 / sizeof(uint32_t),
  46. NPCM7XX_TIMER_TDR4 = 0x0050 / sizeof(uint32_t),
  47. NPCM7XX_TIMER_REGS_END,
  48. };
  49. /* Register field definitions. */
  50. #define NPCM7XX_TCSR_CEN BIT(30)
  51. #define NPCM7XX_TCSR_IE BIT(29)
  52. #define NPCM7XX_TCSR_PERIODIC BIT(27)
  53. #define NPCM7XX_TCSR_CRST BIT(26)
  54. #define NPCM7XX_TCSR_CACT BIT(25)
  55. #define NPCM7XX_TCSR_RSVD 0x01ffff00
  56. #define NPCM7XX_TCSR_PRESCALE_START 0
  57. #define NPCM7XX_TCSR_PRESCALE_LEN 8
  58. /*
  59. * Returns the index of timer in the tc->timer array. This can be used to
  60. * locate the registers that belong to this timer.
  61. */
  62. static int npcm7xx_timer_index(NPCM7xxTimerCtrlState *tc, NPCM7xxTimer *timer)
  63. {
  64. int index = timer - tc->timer;
  65. g_assert(index >= 0 && index < NPCM7XX_TIMERS_PER_CTRL);
  66. return index;
  67. }
  68. /* Return the value by which to divide the reference clock rate. */
  69. static uint32_t npcm7xx_tcsr_prescaler(uint32_t tcsr)
  70. {
  71. return extract32(tcsr, NPCM7XX_TCSR_PRESCALE_START,
  72. NPCM7XX_TCSR_PRESCALE_LEN) + 1;
  73. }
  74. /* Convert a timer cycle count to a time interval in nanoseconds. */
  75. static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer *t, uint32_t count)
  76. {
  77. int64_t ns = count;
  78. ns *= NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ;
  79. ns *= npcm7xx_tcsr_prescaler(t->tcsr);
  80. return ns;
  81. }
  82. /* Convert a time interval in nanoseconds to a timer cycle count. */
  83. static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer *t, int64_t ns)
  84. {
  85. int64_t count;
  86. count = ns / (NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ);
  87. count /= npcm7xx_tcsr_prescaler(t->tcsr);
  88. return count;
  89. }
  90. /*
  91. * Raise the interrupt line if there's a pending interrupt and interrupts are
  92. * enabled for this timer. If not, lower it.
  93. */
  94. static void npcm7xx_timer_check_interrupt(NPCM7xxTimer *t)
  95. {
  96. NPCM7xxTimerCtrlState *tc = t->ctrl;
  97. int index = npcm7xx_timer_index(tc, t);
  98. bool pending = (t->tcsr & NPCM7XX_TCSR_IE) && (tc->tisr & BIT(index));
  99. qemu_set_irq(t->irq, pending);
  100. trace_npcm7xx_timer_irq(DEVICE(tc)->canonical_path, index, pending);
  101. }
  102. /* Start or resume the timer. */
  103. static void npcm7xx_timer_start(NPCM7xxTimer *t)
  104. {
  105. int64_t now;
  106. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  107. t->expires_ns = now + t->remaining_ns;
  108. timer_mod(&t->qtimer, t->expires_ns);
  109. }
  110. /*
  111. * Called when the counter reaches zero. Sets the interrupt flag, and either
  112. * restarts or disables the timer.
  113. */
  114. static void npcm7xx_timer_reached_zero(NPCM7xxTimer *t)
  115. {
  116. NPCM7xxTimerCtrlState *tc = t->ctrl;
  117. int index = npcm7xx_timer_index(tc, t);
  118. tc->tisr |= BIT(index);
  119. if (t->tcsr & NPCM7XX_TCSR_PERIODIC) {
  120. t->remaining_ns = npcm7xx_timer_count_to_ns(t, t->ticr);
  121. if (t->tcsr & NPCM7XX_TCSR_CEN) {
  122. npcm7xx_timer_start(t);
  123. }
  124. } else {
  125. t->tcsr &= ~(NPCM7XX_TCSR_CEN | NPCM7XX_TCSR_CACT);
  126. }
  127. npcm7xx_timer_check_interrupt(t);
  128. }
  129. /* Stop counting. Record the time remaining so we can continue later. */
  130. static void npcm7xx_timer_pause(NPCM7xxTimer *t)
  131. {
  132. int64_t now;
  133. timer_del(&t->qtimer);
  134. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  135. t->remaining_ns = t->expires_ns - now;
  136. if (t->remaining_ns <= 0) {
  137. npcm7xx_timer_reached_zero(t);
  138. }
  139. }
  140. /*
  141. * Restart the timer from its initial value. If the timer was enabled and stays
  142. * enabled, adjust the QEMU timer according to the new count. If the timer is
  143. * transitioning from disabled to enabled, the caller is expected to start the
  144. * timer later.
  145. */
  146. static void npcm7xx_timer_restart(NPCM7xxTimer *t, uint32_t old_tcsr)
  147. {
  148. t->remaining_ns = npcm7xx_timer_count_to_ns(t, t->ticr);
  149. if (old_tcsr & t->tcsr & NPCM7XX_TCSR_CEN) {
  150. npcm7xx_timer_start(t);
  151. }
  152. }
  153. /* Register read and write handlers */
  154. static uint32_t npcm7xx_timer_read_tdr(NPCM7xxTimer *t)
  155. {
  156. if (t->tcsr & NPCM7XX_TCSR_CEN) {
  157. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  158. return npcm7xx_timer_ns_to_count(t, t->expires_ns - now);
  159. }
  160. return npcm7xx_timer_ns_to_count(t, t->remaining_ns);
  161. }
  162. static void npcm7xx_timer_write_tcsr(NPCM7xxTimer *t, uint32_t new_tcsr)
  163. {
  164. uint32_t old_tcsr = t->tcsr;
  165. uint32_t tdr;
  166. if (new_tcsr & NPCM7XX_TCSR_RSVD) {
  167. qemu_log_mask(LOG_GUEST_ERROR, "%s: reserved bits in 0x%08x ignored\n",
  168. __func__, new_tcsr);
  169. new_tcsr &= ~NPCM7XX_TCSR_RSVD;
  170. }
  171. if (new_tcsr & NPCM7XX_TCSR_CACT) {
  172. qemu_log_mask(LOG_GUEST_ERROR, "%s: read-only bits in 0x%08x ignored\n",
  173. __func__, new_tcsr);
  174. new_tcsr &= ~NPCM7XX_TCSR_CACT;
  175. }
  176. if ((new_tcsr & NPCM7XX_TCSR_CRST) && (new_tcsr & NPCM7XX_TCSR_CEN)) {
  177. qemu_log_mask(LOG_GUEST_ERROR,
  178. "%s: both CRST and CEN set; ignoring CEN.\n",
  179. __func__);
  180. new_tcsr &= ~NPCM7XX_TCSR_CEN;
  181. }
  182. /* Calculate the value of TDR before potentially changing the prescaler. */
  183. tdr = npcm7xx_timer_read_tdr(t);
  184. t->tcsr = (t->tcsr & NPCM7XX_TCSR_CACT) | new_tcsr;
  185. if (npcm7xx_tcsr_prescaler(old_tcsr) != npcm7xx_tcsr_prescaler(new_tcsr)) {
  186. /* Recalculate time remaining based on the current TDR value. */
  187. t->remaining_ns = npcm7xx_timer_count_to_ns(t, tdr);
  188. if (old_tcsr & t->tcsr & NPCM7XX_TCSR_CEN) {
  189. npcm7xx_timer_start(t);
  190. }
  191. }
  192. if ((old_tcsr ^ new_tcsr) & NPCM7XX_TCSR_IE) {
  193. npcm7xx_timer_check_interrupt(t);
  194. }
  195. if (new_tcsr & NPCM7XX_TCSR_CRST) {
  196. npcm7xx_timer_restart(t, old_tcsr);
  197. t->tcsr &= ~NPCM7XX_TCSR_CRST;
  198. }
  199. if ((old_tcsr ^ new_tcsr) & NPCM7XX_TCSR_CEN) {
  200. if (new_tcsr & NPCM7XX_TCSR_CEN) {
  201. t->tcsr |= NPCM7XX_TCSR_CACT;
  202. npcm7xx_timer_start(t);
  203. } else {
  204. t->tcsr &= ~NPCM7XX_TCSR_CACT;
  205. npcm7xx_timer_pause(t);
  206. }
  207. }
  208. }
  209. static void npcm7xx_timer_write_ticr(NPCM7xxTimer *t, uint32_t new_ticr)
  210. {
  211. t->ticr = new_ticr;
  212. npcm7xx_timer_restart(t, t->tcsr);
  213. }
  214. static void npcm7xx_timer_write_tisr(NPCM7xxTimerCtrlState *s, uint32_t value)
  215. {
  216. int i;
  217. s->tisr &= ~value;
  218. for (i = 0; i < ARRAY_SIZE(s->timer); i++) {
  219. if (value & (1U << i)) {
  220. npcm7xx_timer_check_interrupt(&s->timer[i]);
  221. }
  222. }
  223. }
  224. static hwaddr npcm7xx_tcsr_index(hwaddr reg)
  225. {
  226. switch (reg) {
  227. case NPCM7XX_TIMER_TCSR0:
  228. return 0;
  229. case NPCM7XX_TIMER_TCSR1:
  230. return 1;
  231. case NPCM7XX_TIMER_TCSR2:
  232. return 2;
  233. case NPCM7XX_TIMER_TCSR3:
  234. return 3;
  235. case NPCM7XX_TIMER_TCSR4:
  236. return 4;
  237. default:
  238. g_assert_not_reached();
  239. }
  240. }
  241. static hwaddr npcm7xx_ticr_index(hwaddr reg)
  242. {
  243. switch (reg) {
  244. case NPCM7XX_TIMER_TICR0:
  245. return 0;
  246. case NPCM7XX_TIMER_TICR1:
  247. return 1;
  248. case NPCM7XX_TIMER_TICR2:
  249. return 2;
  250. case NPCM7XX_TIMER_TICR3:
  251. return 3;
  252. case NPCM7XX_TIMER_TICR4:
  253. return 4;
  254. default:
  255. g_assert_not_reached();
  256. }
  257. }
  258. static hwaddr npcm7xx_tdr_index(hwaddr reg)
  259. {
  260. switch (reg) {
  261. case NPCM7XX_TIMER_TDR0:
  262. return 0;
  263. case NPCM7XX_TIMER_TDR1:
  264. return 1;
  265. case NPCM7XX_TIMER_TDR2:
  266. return 2;
  267. case NPCM7XX_TIMER_TDR3:
  268. return 3;
  269. case NPCM7XX_TIMER_TDR4:
  270. return 4;
  271. default:
  272. g_assert_not_reached();
  273. }
  274. }
  275. static uint64_t npcm7xx_timer_read(void *opaque, hwaddr offset, unsigned size)
  276. {
  277. NPCM7xxTimerCtrlState *s = opaque;
  278. uint64_t value = 0;
  279. hwaddr reg;
  280. reg = offset / sizeof(uint32_t);
  281. switch (reg) {
  282. case NPCM7XX_TIMER_TCSR0:
  283. case NPCM7XX_TIMER_TCSR1:
  284. case NPCM7XX_TIMER_TCSR2:
  285. case NPCM7XX_TIMER_TCSR3:
  286. case NPCM7XX_TIMER_TCSR4:
  287. value = s->timer[npcm7xx_tcsr_index(reg)].tcsr;
  288. break;
  289. case NPCM7XX_TIMER_TICR0:
  290. case NPCM7XX_TIMER_TICR1:
  291. case NPCM7XX_TIMER_TICR2:
  292. case NPCM7XX_TIMER_TICR3:
  293. case NPCM7XX_TIMER_TICR4:
  294. value = s->timer[npcm7xx_ticr_index(reg)].ticr;
  295. break;
  296. case NPCM7XX_TIMER_TDR0:
  297. case NPCM7XX_TIMER_TDR1:
  298. case NPCM7XX_TIMER_TDR2:
  299. case NPCM7XX_TIMER_TDR3:
  300. case NPCM7XX_TIMER_TDR4:
  301. value = npcm7xx_timer_read_tdr(&s->timer[npcm7xx_tdr_index(reg)]);
  302. break;
  303. case NPCM7XX_TIMER_TISR:
  304. value = s->tisr;
  305. break;
  306. case NPCM7XX_TIMER_WTCR:
  307. value = s->wtcr;
  308. break;
  309. default:
  310. qemu_log_mask(LOG_GUEST_ERROR,
  311. "%s: invalid offset 0x%04" HWADDR_PRIx "\n",
  312. __func__, offset);
  313. break;
  314. }
  315. trace_npcm7xx_timer_read(DEVICE(s)->canonical_path, offset, value);
  316. return value;
  317. }
  318. static void npcm7xx_timer_write(void *opaque, hwaddr offset,
  319. uint64_t v, unsigned size)
  320. {
  321. uint32_t reg = offset / sizeof(uint32_t);
  322. NPCM7xxTimerCtrlState *s = opaque;
  323. uint32_t value = v;
  324. trace_npcm7xx_timer_write(DEVICE(s)->canonical_path, offset, value);
  325. switch (reg) {
  326. case NPCM7XX_TIMER_TCSR0:
  327. case NPCM7XX_TIMER_TCSR1:
  328. case NPCM7XX_TIMER_TCSR2:
  329. case NPCM7XX_TIMER_TCSR3:
  330. case NPCM7XX_TIMER_TCSR4:
  331. npcm7xx_timer_write_tcsr(&s->timer[npcm7xx_tcsr_index(reg)], value);
  332. return;
  333. case NPCM7XX_TIMER_TICR0:
  334. case NPCM7XX_TIMER_TICR1:
  335. case NPCM7XX_TIMER_TICR2:
  336. case NPCM7XX_TIMER_TICR3:
  337. case NPCM7XX_TIMER_TICR4:
  338. npcm7xx_timer_write_ticr(&s->timer[npcm7xx_ticr_index(reg)], value);
  339. return;
  340. case NPCM7XX_TIMER_TDR0:
  341. case NPCM7XX_TIMER_TDR1:
  342. case NPCM7XX_TIMER_TDR2:
  343. case NPCM7XX_TIMER_TDR3:
  344. case NPCM7XX_TIMER_TDR4:
  345. qemu_log_mask(LOG_GUEST_ERROR,
  346. "%s: register @ 0x%04" HWADDR_PRIx " is read-only\n",
  347. __func__, offset);
  348. return;
  349. case NPCM7XX_TIMER_TISR:
  350. npcm7xx_timer_write_tisr(s, value);
  351. return;
  352. case NPCM7XX_TIMER_WTCR:
  353. qemu_log_mask(LOG_UNIMP, "%s: WTCR write not implemented: 0x%08x\n",
  354. __func__, value);
  355. return;
  356. }
  357. qemu_log_mask(LOG_GUEST_ERROR,
  358. "%s: invalid offset 0x%04" HWADDR_PRIx "\n",
  359. __func__, offset);
  360. }
  361. static const struct MemoryRegionOps npcm7xx_timer_ops = {
  362. .read = npcm7xx_timer_read,
  363. .write = npcm7xx_timer_write,
  364. .endianness = DEVICE_LITTLE_ENDIAN,
  365. .valid = {
  366. .min_access_size = 4,
  367. .max_access_size = 4,
  368. .unaligned = false,
  369. },
  370. };
  371. /* Called when the QEMU timer expires. */
  372. static void npcm7xx_timer_expired(void *opaque)
  373. {
  374. NPCM7xxTimer *t = opaque;
  375. if (t->tcsr & NPCM7XX_TCSR_CEN) {
  376. npcm7xx_timer_reached_zero(t);
  377. }
  378. }
  379. static void npcm7xx_timer_enter_reset(Object *obj, ResetType type)
  380. {
  381. NPCM7xxTimerCtrlState *s = NPCM7XX_TIMER(obj);
  382. int i;
  383. for (i = 0; i < NPCM7XX_TIMERS_PER_CTRL; i++) {
  384. NPCM7xxTimer *t = &s->timer[i];
  385. timer_del(&t->qtimer);
  386. t->expires_ns = 0;
  387. t->remaining_ns = 0;
  388. t->tcsr = 0x00000005;
  389. t->ticr = 0x00000000;
  390. }
  391. s->tisr = 0x00000000;
  392. s->wtcr = 0x00000400;
  393. }
  394. static void npcm7xx_timer_hold_reset(Object *obj)
  395. {
  396. NPCM7xxTimerCtrlState *s = NPCM7XX_TIMER(obj);
  397. int i;
  398. for (i = 0; i < NPCM7XX_TIMERS_PER_CTRL; i++) {
  399. qemu_irq_lower(s->timer[i].irq);
  400. }
  401. }
  402. static void npcm7xx_timer_realize(DeviceState *dev, Error **errp)
  403. {
  404. NPCM7xxTimerCtrlState *s = NPCM7XX_TIMER(dev);
  405. SysBusDevice *sbd = &s->parent;
  406. int i;
  407. for (i = 0; i < NPCM7XX_TIMERS_PER_CTRL; i++) {
  408. NPCM7xxTimer *t = &s->timer[i];
  409. t->ctrl = s;
  410. timer_init_ns(&t->qtimer, QEMU_CLOCK_VIRTUAL, npcm7xx_timer_expired, t);
  411. sysbus_init_irq(sbd, &t->irq);
  412. }
  413. memory_region_init_io(&s->iomem, OBJECT(s), &npcm7xx_timer_ops, s,
  414. TYPE_NPCM7XX_TIMER, 4 * KiB);
  415. sysbus_init_mmio(sbd, &s->iomem);
  416. }
  417. static const VMStateDescription vmstate_npcm7xx_timer = {
  418. .name = "npcm7xx-timer",
  419. .version_id = 0,
  420. .minimum_version_id = 0,
  421. .fields = (VMStateField[]) {
  422. VMSTATE_TIMER(qtimer, NPCM7xxTimer),
  423. VMSTATE_INT64(expires_ns, NPCM7xxTimer),
  424. VMSTATE_INT64(remaining_ns, NPCM7xxTimer),
  425. VMSTATE_UINT32(tcsr, NPCM7xxTimer),
  426. VMSTATE_UINT32(ticr, NPCM7xxTimer),
  427. VMSTATE_END_OF_LIST(),
  428. },
  429. };
  430. static const VMStateDescription vmstate_npcm7xx_timer_ctrl = {
  431. .name = "npcm7xx-timer-ctrl",
  432. .version_id = 0,
  433. .minimum_version_id = 0,
  434. .fields = (VMStateField[]) {
  435. VMSTATE_UINT32(tisr, NPCM7xxTimerCtrlState),
  436. VMSTATE_UINT32(wtcr, NPCM7xxTimerCtrlState),
  437. VMSTATE_STRUCT_ARRAY(timer, NPCM7xxTimerCtrlState,
  438. NPCM7XX_TIMERS_PER_CTRL, 0, vmstate_npcm7xx_timer,
  439. NPCM7xxTimer),
  440. VMSTATE_END_OF_LIST(),
  441. },
  442. };
  443. static void npcm7xx_timer_class_init(ObjectClass *klass, void *data)
  444. {
  445. ResettableClass *rc = RESETTABLE_CLASS(klass);
  446. DeviceClass *dc = DEVICE_CLASS(klass);
  447. QEMU_BUILD_BUG_ON(NPCM7XX_TIMER_REGS_END > NPCM7XX_TIMER_NR_REGS);
  448. dc->desc = "NPCM7xx Timer Controller";
  449. dc->realize = npcm7xx_timer_realize;
  450. dc->vmsd = &vmstate_npcm7xx_timer_ctrl;
  451. rc->phases.enter = npcm7xx_timer_enter_reset;
  452. rc->phases.hold = npcm7xx_timer_hold_reset;
  453. }
  454. static const TypeInfo npcm7xx_timer_info = {
  455. .name = TYPE_NPCM7XX_TIMER,
  456. .parent = TYPE_SYS_BUS_DEVICE,
  457. .instance_size = sizeof(NPCM7xxTimerCtrlState),
  458. .class_init = npcm7xx_timer_class_init,
  459. };
  460. static void npcm7xx_timer_register_type(void)
  461. {
  462. type_register_static(&npcm7xx_timer_info);
  463. }
  464. type_init(npcm7xx_timer_register_type);