aspeed_timer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * ASPEED AST2400 Timer
  3. *
  4. * Andrew Jeffery <andrew@aj.id.au>
  5. *
  6. * Copyright (C) 2016 IBM Corp.
  7. *
  8. * This code is licensed under the GPL version 2 or later. See
  9. * the COPYING file in the top-level directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qapi/error.h"
  13. #include "hw/irq.h"
  14. #include "hw/sysbus.h"
  15. #include "hw/timer/aspeed_timer.h"
  16. #include "migration/vmstate.h"
  17. #include "qemu/bitops.h"
  18. #include "qemu/timer.h"
  19. #include "qemu/log.h"
  20. #include "qemu/module.h"
  21. #include "hw/qdev-properties.h"
  22. #include "trace.h"
  23. #define TIMER_NR_REGS 4
  24. #define TIMER_CTRL_BITS 4
  25. #define TIMER_CTRL_MASK ((1 << TIMER_CTRL_BITS) - 1)
  26. #define TIMER_CLOCK_USE_EXT true
  27. #define TIMER_CLOCK_EXT_HZ 1000000
  28. #define TIMER_CLOCK_USE_APB false
  29. #define TIMER_REG_STATUS 0
  30. #define TIMER_REG_RELOAD 1
  31. #define TIMER_REG_MATCH_FIRST 2
  32. #define TIMER_REG_MATCH_SECOND 3
  33. #define TIMER_FIRST_CAP_PULSE 4
  34. enum timer_ctrl_op {
  35. op_enable = 0,
  36. op_external_clock,
  37. op_overflow_interrupt,
  38. op_pulse_enable
  39. };
  40. /*
  41. * Minimum value of the reload register to filter out short period
  42. * timers which have a noticeable impact in emulation. 5us should be
  43. * enough, use 20us for "safety".
  44. */
  45. #define TIMER_MIN_NS (20 * SCALE_US)
  46. /**
  47. * Avoid mutual references between AspeedTimerCtrlState and AspeedTimer
  48. * structs, as it's a waste of memory. The ptimer BH callback needs to know
  49. * whether a specific AspeedTimer is enabled, but this information is held in
  50. * AspeedTimerCtrlState. So, provide a helper to hoist ourselves from an
  51. * arbitrary AspeedTimer to AspeedTimerCtrlState.
  52. */
  53. static inline AspeedTimerCtrlState *timer_to_ctrl(AspeedTimer *t)
  54. {
  55. const AspeedTimer (*timers)[] = (void *)t - (t->id * sizeof(*t));
  56. return container_of(timers, AspeedTimerCtrlState, timers);
  57. }
  58. static inline bool timer_ctrl_status(AspeedTimer *t, enum timer_ctrl_op op)
  59. {
  60. return !!(timer_to_ctrl(t)->ctrl & BIT(t->id * TIMER_CTRL_BITS + op));
  61. }
  62. static inline bool timer_enabled(AspeedTimer *t)
  63. {
  64. return timer_ctrl_status(t, op_enable);
  65. }
  66. static inline bool timer_overflow_interrupt(AspeedTimer *t)
  67. {
  68. return timer_ctrl_status(t, op_overflow_interrupt);
  69. }
  70. static inline bool timer_can_pulse(AspeedTimer *t)
  71. {
  72. return t->id >= TIMER_FIRST_CAP_PULSE;
  73. }
  74. static inline bool timer_external_clock(AspeedTimer *t)
  75. {
  76. return timer_ctrl_status(t, op_external_clock);
  77. }
  78. static inline uint32_t calculate_rate(struct AspeedTimer *t)
  79. {
  80. AspeedTimerCtrlState *s = timer_to_ctrl(t);
  81. return timer_external_clock(t) ? TIMER_CLOCK_EXT_HZ :
  82. aspeed_scu_get_apb_freq(s->scu);
  83. }
  84. static inline uint32_t calculate_ticks(struct AspeedTimer *t, uint64_t now_ns)
  85. {
  86. uint64_t delta_ns = now_ns - MIN(now_ns, t->start);
  87. uint32_t rate = calculate_rate(t);
  88. uint64_t ticks = muldiv64(delta_ns, rate, NANOSECONDS_PER_SECOND);
  89. return t->reload - MIN(t->reload, ticks);
  90. }
  91. static uint32_t calculate_min_ticks(AspeedTimer *t, uint32_t value)
  92. {
  93. uint32_t rate = calculate_rate(t);
  94. uint32_t min_ticks = muldiv64(TIMER_MIN_NS, rate, NANOSECONDS_PER_SECOND);
  95. return value < min_ticks ? min_ticks : value;
  96. }
  97. static inline uint64_t calculate_time(struct AspeedTimer *t, uint32_t ticks)
  98. {
  99. uint64_t delta_ns;
  100. uint64_t delta_ticks;
  101. delta_ticks = t->reload - MIN(t->reload, ticks);
  102. delta_ns = muldiv64(delta_ticks, NANOSECONDS_PER_SECOND, calculate_rate(t));
  103. return t->start + delta_ns;
  104. }
  105. static inline uint32_t calculate_match(struct AspeedTimer *t, int i)
  106. {
  107. return t->match[i] < t->reload ? t->match[i] : 0;
  108. }
  109. static uint64_t calculate_next(struct AspeedTimer *t)
  110. {
  111. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  112. uint64_t next;
  113. /*
  114. * We don't know the relationship between the values in the match
  115. * registers, so sort using MAX/MIN/zero. We sort in that order as
  116. * the timer counts down to zero.
  117. */
  118. next = calculate_time(t, MAX(calculate_match(t, 0), calculate_match(t, 1)));
  119. if (now < next) {
  120. return next;
  121. }
  122. next = calculate_time(t, MIN(calculate_match(t, 0), calculate_match(t, 1)));
  123. if (now < next) {
  124. return next;
  125. }
  126. next = calculate_time(t, 0);
  127. if (now < next) {
  128. return next;
  129. }
  130. /* We've missed all deadlines, fire interrupt and try again */
  131. timer_del(&t->timer);
  132. if (timer_overflow_interrupt(t)) {
  133. AspeedTimerCtrlState *s = timer_to_ctrl(t);
  134. t->level = !t->level;
  135. s->irq_sts |= BIT(t->id);
  136. qemu_set_irq(t->irq, t->level);
  137. }
  138. next = MAX(calculate_match(t, 0), calculate_match(t, 1));
  139. t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  140. return calculate_time(t, next);
  141. }
  142. static void aspeed_timer_mod(AspeedTimer *t)
  143. {
  144. uint64_t next = calculate_next(t);
  145. if (next) {
  146. timer_mod(&t->timer, next);
  147. }
  148. }
  149. static void aspeed_timer_expire(void *opaque)
  150. {
  151. AspeedTimer *t = opaque;
  152. bool interrupt = false;
  153. uint32_t ticks;
  154. if (!timer_enabled(t)) {
  155. return;
  156. }
  157. ticks = calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  158. if (!ticks) {
  159. interrupt = timer_overflow_interrupt(t) || !t->match[0] || !t->match[1];
  160. } else if (ticks <= MIN(t->match[0], t->match[1])) {
  161. interrupt = true;
  162. } else if (ticks <= MAX(t->match[0], t->match[1])) {
  163. interrupt = true;
  164. }
  165. if (interrupt) {
  166. AspeedTimerCtrlState *s = timer_to_ctrl(t);
  167. t->level = !t->level;
  168. s->irq_sts |= BIT(t->id);
  169. qemu_set_irq(t->irq, t->level);
  170. }
  171. aspeed_timer_mod(t);
  172. }
  173. static uint64_t aspeed_timer_get_value(AspeedTimer *t, int reg)
  174. {
  175. uint64_t value;
  176. switch (reg) {
  177. case TIMER_REG_STATUS:
  178. if (timer_enabled(t)) {
  179. value = calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  180. } else {
  181. value = t->reload;
  182. }
  183. break;
  184. case TIMER_REG_RELOAD:
  185. value = t->reload;
  186. break;
  187. case TIMER_REG_MATCH_FIRST:
  188. case TIMER_REG_MATCH_SECOND:
  189. value = t->match[reg - 2];
  190. break;
  191. default:
  192. qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
  193. __func__, reg);
  194. value = 0;
  195. break;
  196. }
  197. return value;
  198. }
  199. static uint64_t aspeed_timer_read_common(AspeedTimerCtrlState *s, hwaddr offset)
  200. {
  201. const int reg = (offset & 0xf) / 4;
  202. uint64_t value;
  203. switch (offset) {
  204. case 0x30: /* Control Register */
  205. value = s->ctrl;
  206. break;
  207. case 0x00 ... 0x2c: /* Timers 1 - 4 */
  208. value = aspeed_timer_get_value(&s->timers[(offset >> 4)], reg);
  209. break;
  210. case 0x40 ... 0x8c: /* Timers 5 - 8 */
  211. value = aspeed_timer_get_value(&s->timers[(offset >> 4) - 1], reg);
  212. break;
  213. default:
  214. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  215. __func__, offset);
  216. value = 0;
  217. break;
  218. }
  219. return value;
  220. }
  221. static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,
  222. uint32_t value)
  223. {
  224. AspeedTimer *t;
  225. uint32_t old_reload;
  226. trace_aspeed_timer_set_value(timer, reg, value);
  227. t = &s->timers[timer];
  228. switch (reg) {
  229. case TIMER_REG_RELOAD:
  230. old_reload = t->reload;
  231. t->reload = calculate_min_ticks(t, value);
  232. /*
  233. * If the reload value was not previously set, or zero, and
  234. * the current value is valid, try to start the timer if it is
  235. * enabled.
  236. */
  237. if (old_reload || !t->reload) {
  238. break;
  239. }
  240. /* fall through to re-enable */
  241. case TIMER_REG_STATUS:
  242. if (timer_enabled(t)) {
  243. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  244. int64_t delta = (int64_t) value - (int64_t) calculate_ticks(t, now);
  245. uint32_t rate = calculate_rate(t);
  246. if (delta >= 0) {
  247. t->start += muldiv64(delta, NANOSECONDS_PER_SECOND, rate);
  248. } else {
  249. t->start -= muldiv64(-delta, NANOSECONDS_PER_SECOND, rate);
  250. }
  251. aspeed_timer_mod(t);
  252. }
  253. break;
  254. case TIMER_REG_MATCH_FIRST:
  255. case TIMER_REG_MATCH_SECOND:
  256. t->match[reg - 2] = value;
  257. if (timer_enabled(t)) {
  258. aspeed_timer_mod(t);
  259. }
  260. break;
  261. default:
  262. qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
  263. __func__, reg);
  264. break;
  265. }
  266. }
  267. /*
  268. * Control register operations are broken out into helpers that can be
  269. * explicitly called on aspeed_timer_reset(), but also from
  270. * aspeed_timer_ctrl_op().
  271. */
  272. static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)
  273. {
  274. trace_aspeed_timer_ctrl_enable(t->id, enable);
  275. if (enable) {
  276. t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  277. aspeed_timer_mod(t);
  278. } else {
  279. timer_del(&t->timer);
  280. }
  281. }
  282. static void aspeed_timer_ctrl_external_clock(AspeedTimer *t, bool enable)
  283. {
  284. trace_aspeed_timer_ctrl_external_clock(t->id, enable);
  285. }
  286. static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer *t, bool enable)
  287. {
  288. trace_aspeed_timer_ctrl_overflow_interrupt(t->id, enable);
  289. }
  290. static void aspeed_timer_ctrl_pulse_enable(AspeedTimer *t, bool enable)
  291. {
  292. if (timer_can_pulse(t)) {
  293. trace_aspeed_timer_ctrl_pulse_enable(t->id, enable);
  294. } else {
  295. qemu_log_mask(LOG_GUEST_ERROR,
  296. "%s: Timer does not support pulse mode\n", __func__);
  297. }
  298. }
  299. /**
  300. * Given the actions are fixed in number and completely described in helper
  301. * functions, dispatch with a lookup table rather than manage control flow with
  302. * a switch statement.
  303. */
  304. static void (*const ctrl_ops[])(AspeedTimer *, bool) = {
  305. [op_enable] = aspeed_timer_ctrl_enable,
  306. [op_external_clock] = aspeed_timer_ctrl_external_clock,
  307. [op_overflow_interrupt] = aspeed_timer_ctrl_overflow_interrupt,
  308. [op_pulse_enable] = aspeed_timer_ctrl_pulse_enable,
  309. };
  310. /**
  311. * Conditionally affect changes chosen by a timer's control bit.
  312. *
  313. * The aspeed_timer_ctrl_op() interface is convenient for the
  314. * aspeed_timer_set_ctrl() function as the "no change" early exit can be
  315. * calculated for all operations, which cleans up the caller code. However the
  316. * interface isn't convenient for the reset function where we want to enter a
  317. * specific state without artificially constructing old and new values that
  318. * will fall through the change guard (and motivates extracting the actions
  319. * out to helper functions).
  320. *
  321. * @t: The timer to manipulate
  322. * @op: The type of operation to be performed
  323. * @old: The old state of the timer's control bits
  324. * @new: The incoming state for the timer's control bits
  325. */
  326. static void aspeed_timer_ctrl_op(AspeedTimer *t, enum timer_ctrl_op op,
  327. uint8_t old, uint8_t new)
  328. {
  329. const uint8_t mask = BIT(op);
  330. const bool enable = !!(new & mask);
  331. const bool changed = ((old ^ new) & mask);
  332. if (!changed) {
  333. return;
  334. }
  335. ctrl_ops[op](t, enable);
  336. }
  337. static void aspeed_timer_set_ctrl(AspeedTimerCtrlState *s, uint32_t reg)
  338. {
  339. int i;
  340. int shift;
  341. uint8_t t_old, t_new;
  342. AspeedTimer *t;
  343. const uint8_t enable_mask = BIT(op_enable);
  344. /*
  345. * Handle a dependency between the 'enable' and remaining three
  346. * configuration bits - i.e. if more than one bit in the control set has
  347. * changed, including the 'enable' bit, then we want either disable the
  348. * timer and perform configuration, or perform configuration and then
  349. * enable the timer
  350. */
  351. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  352. t = &s->timers[i];
  353. shift = (i * TIMER_CTRL_BITS);
  354. t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK;
  355. t_new = (reg >> shift) & TIMER_CTRL_MASK;
  356. /* If we are disabling, do so first */
  357. if ((t_old & enable_mask) && !(t_new & enable_mask)) {
  358. aspeed_timer_ctrl_enable(t, false);
  359. }
  360. aspeed_timer_ctrl_op(t, op_external_clock, t_old, t_new);
  361. aspeed_timer_ctrl_op(t, op_overflow_interrupt, t_old, t_new);
  362. aspeed_timer_ctrl_op(t, op_pulse_enable, t_old, t_new);
  363. /* If we are enabling, do so last */
  364. if (!(t_old & enable_mask) && (t_new & enable_mask)) {
  365. aspeed_timer_ctrl_enable(t, true);
  366. }
  367. }
  368. s->ctrl = reg;
  369. }
  370. static void aspeed_timer_set_ctrl2(AspeedTimerCtrlState *s, uint32_t value)
  371. {
  372. trace_aspeed_timer_set_ctrl2(value);
  373. }
  374. static void aspeed_timer_write_common(AspeedTimerCtrlState *s, hwaddr offset,
  375. uint64_t value)
  376. {
  377. const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
  378. const int reg = (offset & 0xf) / 4;
  379. switch (offset) {
  380. /* Control Registers */
  381. case 0x30:
  382. aspeed_timer_set_ctrl(s, tv);
  383. break;
  384. /* Timer Registers */
  385. case 0x00 ... 0x2c:
  386. aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS), reg, tv);
  387. break;
  388. case 0x40 ... 0x8c:
  389. aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS) - 1, reg, tv);
  390. break;
  391. default:
  392. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  393. __func__, offset);
  394. break;
  395. }
  396. }
  397. static uint64_t aspeed_timer_read(void *opaque, hwaddr offset, unsigned size)
  398. {
  399. AspeedTimerCtrlState *s = ASPEED_TIMER(opaque);
  400. return ASPEED_TIMER_GET_CLASS(s)->read(s, offset);
  401. }
  402. static void aspeed_timer_write(void *opaque, hwaddr offset, uint64_t value,
  403. unsigned size)
  404. {
  405. AspeedTimerCtrlState *s = ASPEED_TIMER(opaque);
  406. ASPEED_TIMER_GET_CLASS(s)->write(s, offset, value);
  407. }
  408. static const MemoryRegionOps aspeed_timer_ops = {
  409. .read = aspeed_timer_read,
  410. .write = aspeed_timer_write,
  411. .endianness = DEVICE_LITTLE_ENDIAN,
  412. .valid.min_access_size = 4,
  413. .valid.max_access_size = 4,
  414. .valid.unaligned = false,
  415. };
  416. static uint64_t aspeed_2400_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
  417. {
  418. uint64_t value;
  419. switch (offset) {
  420. case 0x34:
  421. value = s->ctrl2;
  422. break;
  423. case 0x38:
  424. case 0x3C:
  425. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  426. __func__, offset);
  427. value = 0;
  428. break;
  429. default:
  430. value = aspeed_timer_read_common(s, offset);
  431. break;
  432. }
  433. trace_aspeed_timer_read(offset, value);
  434. return value;
  435. }
  436. static void aspeed_2400_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
  437. uint64_t value)
  438. {
  439. const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
  440. switch (offset) {
  441. case 0x34:
  442. aspeed_timer_set_ctrl2(s, tv);
  443. break;
  444. case 0x38:
  445. case 0x3C:
  446. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  447. __func__, offset);
  448. break;
  449. default:
  450. aspeed_timer_write_common(s, offset, value);
  451. break;
  452. }
  453. }
  454. static uint64_t aspeed_2500_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
  455. {
  456. uint64_t value;
  457. switch (offset) {
  458. case 0x34:
  459. value = s->ctrl2;
  460. break;
  461. case 0x38:
  462. value = s->ctrl3 & BIT(0);
  463. break;
  464. case 0x3C:
  465. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  466. __func__, offset);
  467. value = 0;
  468. break;
  469. default:
  470. value = aspeed_timer_read_common(s, offset);
  471. break;
  472. }
  473. trace_aspeed_timer_read(offset, value);
  474. return value;
  475. }
  476. static void aspeed_2500_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
  477. uint64_t value)
  478. {
  479. const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
  480. uint8_t command;
  481. switch (offset) {
  482. case 0x34:
  483. aspeed_timer_set_ctrl2(s, tv);
  484. break;
  485. case 0x38:
  486. command = (value >> 1) & 0xFF;
  487. if (command == 0xAE) {
  488. s->ctrl3 = 0x1;
  489. } else if (command == 0xEA) {
  490. s->ctrl3 = 0x0;
  491. }
  492. break;
  493. case 0x3C:
  494. if (s->ctrl3 & BIT(0)) {
  495. aspeed_timer_set_ctrl(s, s->ctrl & ~tv);
  496. }
  497. break;
  498. default:
  499. aspeed_timer_write_common(s, offset, value);
  500. break;
  501. }
  502. }
  503. static uint64_t aspeed_2600_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
  504. {
  505. uint64_t value;
  506. switch (offset) {
  507. case 0x34:
  508. value = s->irq_sts;
  509. break;
  510. case 0x38:
  511. case 0x3C:
  512. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  513. __func__, offset);
  514. value = 0;
  515. break;
  516. default:
  517. value = aspeed_timer_read_common(s, offset);
  518. break;
  519. }
  520. trace_aspeed_timer_read(offset, value);
  521. return value;
  522. }
  523. static void aspeed_2600_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
  524. uint64_t value)
  525. {
  526. const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
  527. switch (offset) {
  528. case 0x34:
  529. s->irq_sts &= ~tv;
  530. break;
  531. case 0x3C:
  532. aspeed_timer_set_ctrl(s, s->ctrl & ~tv);
  533. break;
  534. case 0x38:
  535. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  536. __func__, offset);
  537. break;
  538. default:
  539. aspeed_timer_write_common(s, offset, value);
  540. break;
  541. }
  542. }
  543. static void aspeed_init_one_timer(AspeedTimerCtrlState *s, uint8_t id)
  544. {
  545. AspeedTimer *t = &s->timers[id];
  546. t->id = id;
  547. timer_init_ns(&t->timer, QEMU_CLOCK_VIRTUAL, aspeed_timer_expire, t);
  548. }
  549. static void aspeed_timer_realize(DeviceState *dev, Error **errp)
  550. {
  551. int i;
  552. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  553. AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
  554. assert(s->scu);
  555. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  556. aspeed_init_one_timer(s, i);
  557. sysbus_init_irq(sbd, &s->timers[i].irq);
  558. }
  559. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_timer_ops, s,
  560. TYPE_ASPEED_TIMER, 0x1000);
  561. sysbus_init_mmio(sbd, &s->iomem);
  562. }
  563. static void aspeed_timer_reset(DeviceState *dev)
  564. {
  565. int i;
  566. AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
  567. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  568. AspeedTimer *t = &s->timers[i];
  569. /*
  570. * Explicitly call helpers to avoid any conditional behaviour through
  571. * aspeed_timer_set_ctrl().
  572. */
  573. aspeed_timer_ctrl_enable(t, false);
  574. aspeed_timer_ctrl_external_clock(t, TIMER_CLOCK_USE_APB);
  575. aspeed_timer_ctrl_overflow_interrupt(t, false);
  576. aspeed_timer_ctrl_pulse_enable(t, false);
  577. t->level = 0;
  578. t->reload = 0;
  579. t->match[0] = 0;
  580. t->match[1] = 0;
  581. }
  582. s->ctrl = 0;
  583. s->ctrl2 = 0;
  584. s->ctrl3 = 0;
  585. s->irq_sts = 0;
  586. }
  587. static const VMStateDescription vmstate_aspeed_timer = {
  588. .name = "aspeed.timer",
  589. .version_id = 2,
  590. .minimum_version_id = 2,
  591. .fields = (const VMStateField[]) {
  592. VMSTATE_UINT8(id, AspeedTimer),
  593. VMSTATE_INT32(level, AspeedTimer),
  594. VMSTATE_TIMER(timer, AspeedTimer),
  595. VMSTATE_UINT32(reload, AspeedTimer),
  596. VMSTATE_UINT32_ARRAY(match, AspeedTimer, 2),
  597. VMSTATE_END_OF_LIST()
  598. }
  599. };
  600. static const VMStateDescription vmstate_aspeed_timer_state = {
  601. .name = "aspeed.timerctrl",
  602. .version_id = 2,
  603. .minimum_version_id = 2,
  604. .fields = (const VMStateField[]) {
  605. VMSTATE_UINT32(ctrl, AspeedTimerCtrlState),
  606. VMSTATE_UINT32(ctrl2, AspeedTimerCtrlState),
  607. VMSTATE_UINT32(ctrl3, AspeedTimerCtrlState),
  608. VMSTATE_UINT32(irq_sts, AspeedTimerCtrlState),
  609. VMSTATE_STRUCT_ARRAY(timers, AspeedTimerCtrlState,
  610. ASPEED_TIMER_NR_TIMERS, 1, vmstate_aspeed_timer,
  611. AspeedTimer),
  612. VMSTATE_END_OF_LIST()
  613. }
  614. };
  615. static const Property aspeed_timer_properties[] = {
  616. DEFINE_PROP_LINK("scu", AspeedTimerCtrlState, scu, TYPE_ASPEED_SCU,
  617. AspeedSCUState *),
  618. };
  619. static void timer_class_init(ObjectClass *klass, void *data)
  620. {
  621. DeviceClass *dc = DEVICE_CLASS(klass);
  622. dc->realize = aspeed_timer_realize;
  623. device_class_set_legacy_reset(dc, aspeed_timer_reset);
  624. dc->desc = "ASPEED Timer";
  625. dc->vmsd = &vmstate_aspeed_timer_state;
  626. device_class_set_props(dc, aspeed_timer_properties);
  627. }
  628. static const TypeInfo aspeed_timer_info = {
  629. .name = TYPE_ASPEED_TIMER,
  630. .parent = TYPE_SYS_BUS_DEVICE,
  631. .instance_size = sizeof(AspeedTimerCtrlState),
  632. .class_init = timer_class_init,
  633. .class_size = sizeof(AspeedTimerClass),
  634. .abstract = true,
  635. };
  636. static void aspeed_2400_timer_class_init(ObjectClass *klass, void *data)
  637. {
  638. DeviceClass *dc = DEVICE_CLASS(klass);
  639. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  640. dc->desc = "ASPEED 2400 Timer";
  641. awc->read = aspeed_2400_timer_read;
  642. awc->write = aspeed_2400_timer_write;
  643. }
  644. static const TypeInfo aspeed_2400_timer_info = {
  645. .name = TYPE_ASPEED_2400_TIMER,
  646. .parent = TYPE_ASPEED_TIMER,
  647. .class_init = aspeed_2400_timer_class_init,
  648. };
  649. static void aspeed_2500_timer_class_init(ObjectClass *klass, void *data)
  650. {
  651. DeviceClass *dc = DEVICE_CLASS(klass);
  652. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  653. dc->desc = "ASPEED 2500 Timer";
  654. awc->read = aspeed_2500_timer_read;
  655. awc->write = aspeed_2500_timer_write;
  656. }
  657. static const TypeInfo aspeed_2500_timer_info = {
  658. .name = TYPE_ASPEED_2500_TIMER,
  659. .parent = TYPE_ASPEED_TIMER,
  660. .class_init = aspeed_2500_timer_class_init,
  661. };
  662. static void aspeed_2600_timer_class_init(ObjectClass *klass, void *data)
  663. {
  664. DeviceClass *dc = DEVICE_CLASS(klass);
  665. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  666. dc->desc = "ASPEED 2600 Timer";
  667. awc->read = aspeed_2600_timer_read;
  668. awc->write = aspeed_2600_timer_write;
  669. }
  670. static const TypeInfo aspeed_2600_timer_info = {
  671. .name = TYPE_ASPEED_2600_TIMER,
  672. .parent = TYPE_ASPEED_TIMER,
  673. .class_init = aspeed_2600_timer_class_init,
  674. };
  675. static void aspeed_1030_timer_class_init(ObjectClass *klass, void *data)
  676. {
  677. DeviceClass *dc = DEVICE_CLASS(klass);
  678. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  679. dc->desc = "ASPEED 1030 Timer";
  680. awc->read = aspeed_2600_timer_read;
  681. awc->write = aspeed_2600_timer_write;
  682. }
  683. static const TypeInfo aspeed_1030_timer_info = {
  684. .name = TYPE_ASPEED_1030_TIMER,
  685. .parent = TYPE_ASPEED_TIMER,
  686. .class_init = aspeed_1030_timer_class_init,
  687. };
  688. static void aspeed_timer_register_types(void)
  689. {
  690. type_register_static(&aspeed_timer_info);
  691. type_register_static(&aspeed_2400_timer_info);
  692. type_register_static(&aspeed_2500_timer_info);
  693. type_register_static(&aspeed_2600_timer_info);
  694. type_register_static(&aspeed_1030_timer_info);
  695. }
  696. type_init(aspeed_timer_register_types)