aspeed_timer.c 21 KB

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