aspeed_timer.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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_2700_timer_set_ctrl(AspeedTimerCtrlState *s, int index,
  544. uint32_t reg)
  545. {
  546. const uint8_t overflow_interrupt_mask = BIT(op_overflow_interrupt);
  547. const uint8_t external_clock_mask = BIT(op_external_clock);
  548. const uint8_t pulse_enable_mask = BIT(op_pulse_enable);
  549. const uint8_t enable_mask = BIT(op_enable);
  550. AspeedTimer *t;
  551. uint8_t t_old;
  552. uint8_t t_new;
  553. int shift;
  554. /*
  555. * Only 1 will set the specific bits to 1
  556. * Handle a dependency between the 'enable' and remaining three
  557. * configuration bits - i.e. if more than one bit in the control set has
  558. * set, including the 'enable' bit, perform configuration and then
  559. * enable the timer.
  560. * Interrupt Status bit should not be set.
  561. */
  562. t = &s->timers[index];
  563. shift = index * TIMER_CTRL_BITS;
  564. t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK;
  565. t_new = reg & TIMER_CTRL_MASK;
  566. if (!(t_old & external_clock_mask) &&
  567. (t_new & external_clock_mask)) {
  568. aspeed_timer_ctrl_external_clock(t, true);
  569. s->ctrl = deposit32(s->ctrl, shift + op_external_clock, 1, 1);
  570. }
  571. if (!(t_old & overflow_interrupt_mask) &&
  572. (t_new & overflow_interrupt_mask)) {
  573. aspeed_timer_ctrl_overflow_interrupt(t, true);
  574. s->ctrl = deposit32(s->ctrl, shift + op_overflow_interrupt, 1, 1);
  575. }
  576. if (!(t_old & pulse_enable_mask) &&
  577. (t_new & pulse_enable_mask)) {
  578. aspeed_timer_ctrl_pulse_enable(t, true);
  579. s->ctrl = deposit32(s->ctrl, shift + op_pulse_enable, 1, 1);
  580. }
  581. /* If we are enabling, do so last */
  582. if (!(t_old & enable_mask) &&
  583. (t_new & enable_mask)) {
  584. aspeed_timer_ctrl_enable(t, true);
  585. s->ctrl = deposit32(s->ctrl, shift + op_enable, 1, 1);
  586. }
  587. }
  588. static void aspeed_2700_timer_clear_ctrl(AspeedTimerCtrlState *s, int index,
  589. uint32_t reg)
  590. {
  591. const uint8_t overflow_interrupt_mask = BIT(op_overflow_interrupt);
  592. const uint8_t external_clock_mask = BIT(op_external_clock);
  593. const uint8_t pulse_enable_mask = BIT(op_pulse_enable);
  594. const uint8_t enable_mask = BIT(op_enable);
  595. AspeedTimer *t;
  596. uint8_t t_old;
  597. uint8_t t_new;
  598. int shift;
  599. /*
  600. * Only 1 will clear the specific bits to 0
  601. * Handle a dependency between the 'enable' and remaining three
  602. * configuration bits - i.e. if more than one bit in the control set has
  603. * clear, including the 'enable' bit, then disable the timer and perform
  604. * configuration
  605. */
  606. t = &s->timers[index];
  607. shift = index * TIMER_CTRL_BITS;
  608. t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK;
  609. t_new = reg & TIMER_CTRL_MASK;
  610. /* If we are disabling, do so first */
  611. if ((t_old & enable_mask) &&
  612. (t_new & enable_mask)) {
  613. aspeed_timer_ctrl_enable(t, false);
  614. s->ctrl = deposit32(s->ctrl, shift + op_enable, 1, 0);
  615. }
  616. if ((t_old & external_clock_mask) &&
  617. (t_new & external_clock_mask)) {
  618. aspeed_timer_ctrl_external_clock(t, false);
  619. s->ctrl = deposit32(s->ctrl, shift + op_external_clock, 1, 0);
  620. }
  621. if ((t_old & overflow_interrupt_mask) &&
  622. (t_new & overflow_interrupt_mask)) {
  623. aspeed_timer_ctrl_overflow_interrupt(t, false);
  624. s->ctrl = deposit32(s->ctrl, shift + op_overflow_interrupt, 1, 0);
  625. }
  626. if ((t_old & pulse_enable_mask) &&
  627. (t_new & pulse_enable_mask)) {
  628. aspeed_timer_ctrl_pulse_enable(t, false);
  629. s->ctrl = deposit32(s->ctrl, shift + op_pulse_enable, 1, 0);
  630. }
  631. /* Clear interrupt status */
  632. if (reg & 0x10000) {
  633. s->irq_sts = deposit32(s->irq_sts, index, 1, 0);
  634. }
  635. }
  636. static uint64_t aspeed_2700_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
  637. {
  638. uint32_t timer_offset = offset & 0x3f;
  639. int timer_index = offset >> 6;
  640. uint64_t value = 0;
  641. if (timer_index >= ASPEED_TIMER_NR_TIMERS) {
  642. qemu_log_mask(LOG_GUEST_ERROR,
  643. "%s: offset 0x%" PRIx64 " out of bounds\n",
  644. __func__, offset);
  645. return 0;
  646. }
  647. switch (timer_offset) {
  648. /*
  649. * Counter Status
  650. * Counter Reload
  651. * Counter First Matching
  652. * Counter Second Matching
  653. */
  654. case 0x00 ... 0x0C:
  655. value = aspeed_timer_get_value(&s->timers[timer_index],
  656. timer_offset >> 2);
  657. break;
  658. /* Counter Control and Interrupt Status */
  659. case 0x10:
  660. value = deposit64(value, 0, 4,
  661. extract32(s->ctrl, timer_index * 4, 4));
  662. value = deposit64(value, 16, 1,
  663. extract32(s->irq_sts, timer_index, 1));
  664. break;
  665. default:
  666. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  667. PRIx64"\n", __func__, offset);
  668. value = 0;
  669. break;
  670. }
  671. trace_aspeed_timer_read(offset, value);
  672. return value;
  673. }
  674. static void aspeed_2700_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
  675. uint64_t value)
  676. {
  677. const uint32_t timer_value = (uint32_t)(value & 0xFFFFFFFF);
  678. uint32_t timer_offset = offset & 0x3f;
  679. int timer_index = offset >> 6;
  680. if (timer_index >= ASPEED_TIMER_NR_TIMERS) {
  681. qemu_log_mask(LOG_GUEST_ERROR,
  682. "%s: offset 0x%" PRIx64 " out of bounds\n",
  683. __func__, offset);
  684. }
  685. switch (timer_offset) {
  686. /*
  687. * Counter Status
  688. * Counter Reload
  689. * Counter First Matching
  690. * Counter Second Matching
  691. */
  692. case 0x00 ... 0x0C:
  693. aspeed_timer_set_value(s, timer_index, timer_offset >> 2,
  694. timer_value);
  695. break;
  696. /* Counter Control Set and Interrupt Status */
  697. case 0x10:
  698. aspeed_2700_timer_set_ctrl(s, timer_index, timer_value);
  699. break;
  700. /* Counter Control Clear and Interrupr Status */
  701. case 0x14:
  702. aspeed_2700_timer_clear_ctrl(s, timer_index, timer_value);
  703. break;
  704. default:
  705. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  706. PRIx64"\n", __func__, offset);
  707. break;
  708. }
  709. }
  710. static void aspeed_init_one_timer(AspeedTimerCtrlState *s, uint8_t id)
  711. {
  712. AspeedTimer *t = &s->timers[id];
  713. t->id = id;
  714. timer_init_ns(&t->timer, QEMU_CLOCK_VIRTUAL, aspeed_timer_expire, t);
  715. }
  716. static void aspeed_timer_realize(DeviceState *dev, Error **errp)
  717. {
  718. int i;
  719. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  720. AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
  721. assert(s->scu);
  722. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  723. aspeed_init_one_timer(s, i);
  724. sysbus_init_irq(sbd, &s->timers[i].irq);
  725. }
  726. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_timer_ops, s,
  727. TYPE_ASPEED_TIMER, 0x1000);
  728. sysbus_init_mmio(sbd, &s->iomem);
  729. }
  730. static void aspeed_timer_reset(DeviceState *dev)
  731. {
  732. int i;
  733. AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
  734. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  735. AspeedTimer *t = &s->timers[i];
  736. /*
  737. * Explicitly call helpers to avoid any conditional behaviour through
  738. * aspeed_timer_set_ctrl().
  739. */
  740. aspeed_timer_ctrl_enable(t, false);
  741. aspeed_timer_ctrl_external_clock(t, TIMER_CLOCK_USE_APB);
  742. aspeed_timer_ctrl_overflow_interrupt(t, false);
  743. aspeed_timer_ctrl_pulse_enable(t, false);
  744. t->level = 0;
  745. t->reload = 0;
  746. t->match[0] = 0;
  747. t->match[1] = 0;
  748. }
  749. s->ctrl = 0;
  750. s->ctrl2 = 0;
  751. s->ctrl3 = 0;
  752. s->irq_sts = 0;
  753. }
  754. static const VMStateDescription vmstate_aspeed_timer = {
  755. .name = "aspeed.timer",
  756. .version_id = 2,
  757. .minimum_version_id = 2,
  758. .fields = (const VMStateField[]) {
  759. VMSTATE_UINT8(id, AspeedTimer),
  760. VMSTATE_INT32(level, AspeedTimer),
  761. VMSTATE_TIMER(timer, AspeedTimer),
  762. VMSTATE_UINT32(reload, AspeedTimer),
  763. VMSTATE_UINT32_ARRAY(match, AspeedTimer, 2),
  764. VMSTATE_END_OF_LIST()
  765. }
  766. };
  767. static const VMStateDescription vmstate_aspeed_timer_state = {
  768. .name = "aspeed.timerctrl",
  769. .version_id = 2,
  770. .minimum_version_id = 2,
  771. .fields = (const VMStateField[]) {
  772. VMSTATE_UINT32(ctrl, AspeedTimerCtrlState),
  773. VMSTATE_UINT32(ctrl2, AspeedTimerCtrlState),
  774. VMSTATE_UINT32(ctrl3, AspeedTimerCtrlState),
  775. VMSTATE_UINT32(irq_sts, AspeedTimerCtrlState),
  776. VMSTATE_STRUCT_ARRAY(timers, AspeedTimerCtrlState,
  777. ASPEED_TIMER_NR_TIMERS, 1, vmstate_aspeed_timer,
  778. AspeedTimer),
  779. VMSTATE_END_OF_LIST()
  780. }
  781. };
  782. static const Property aspeed_timer_properties[] = {
  783. DEFINE_PROP_LINK("scu", AspeedTimerCtrlState, scu, TYPE_ASPEED_SCU,
  784. AspeedSCUState *),
  785. };
  786. static void timer_class_init(ObjectClass *klass, void *data)
  787. {
  788. DeviceClass *dc = DEVICE_CLASS(klass);
  789. dc->realize = aspeed_timer_realize;
  790. device_class_set_legacy_reset(dc, aspeed_timer_reset);
  791. dc->desc = "ASPEED Timer";
  792. dc->vmsd = &vmstate_aspeed_timer_state;
  793. device_class_set_props(dc, aspeed_timer_properties);
  794. }
  795. static const TypeInfo aspeed_timer_info = {
  796. .name = TYPE_ASPEED_TIMER,
  797. .parent = TYPE_SYS_BUS_DEVICE,
  798. .instance_size = sizeof(AspeedTimerCtrlState),
  799. .class_init = timer_class_init,
  800. .class_size = sizeof(AspeedTimerClass),
  801. .abstract = true,
  802. };
  803. static void aspeed_2400_timer_class_init(ObjectClass *klass, void *data)
  804. {
  805. DeviceClass *dc = DEVICE_CLASS(klass);
  806. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  807. dc->desc = "ASPEED 2400 Timer";
  808. awc->read = aspeed_2400_timer_read;
  809. awc->write = aspeed_2400_timer_write;
  810. }
  811. static const TypeInfo aspeed_2400_timer_info = {
  812. .name = TYPE_ASPEED_2400_TIMER,
  813. .parent = TYPE_ASPEED_TIMER,
  814. .class_init = aspeed_2400_timer_class_init,
  815. };
  816. static void aspeed_2500_timer_class_init(ObjectClass *klass, void *data)
  817. {
  818. DeviceClass *dc = DEVICE_CLASS(klass);
  819. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  820. dc->desc = "ASPEED 2500 Timer";
  821. awc->read = aspeed_2500_timer_read;
  822. awc->write = aspeed_2500_timer_write;
  823. }
  824. static const TypeInfo aspeed_2500_timer_info = {
  825. .name = TYPE_ASPEED_2500_TIMER,
  826. .parent = TYPE_ASPEED_TIMER,
  827. .class_init = aspeed_2500_timer_class_init,
  828. };
  829. static void aspeed_2600_timer_class_init(ObjectClass *klass, void *data)
  830. {
  831. DeviceClass *dc = DEVICE_CLASS(klass);
  832. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  833. dc->desc = "ASPEED 2600 Timer";
  834. awc->read = aspeed_2600_timer_read;
  835. awc->write = aspeed_2600_timer_write;
  836. }
  837. static const TypeInfo aspeed_2600_timer_info = {
  838. .name = TYPE_ASPEED_2600_TIMER,
  839. .parent = TYPE_ASPEED_TIMER,
  840. .class_init = aspeed_2600_timer_class_init,
  841. };
  842. static void aspeed_1030_timer_class_init(ObjectClass *klass, void *data)
  843. {
  844. DeviceClass *dc = DEVICE_CLASS(klass);
  845. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  846. dc->desc = "ASPEED 1030 Timer";
  847. awc->read = aspeed_2600_timer_read;
  848. awc->write = aspeed_2600_timer_write;
  849. }
  850. static const TypeInfo aspeed_1030_timer_info = {
  851. .name = TYPE_ASPEED_1030_TIMER,
  852. .parent = TYPE_ASPEED_TIMER,
  853. .class_init = aspeed_1030_timer_class_init,
  854. };
  855. static void aspeed_2700_timer_class_init(ObjectClass *klass, void *data)
  856. {
  857. DeviceClass *dc = DEVICE_CLASS(klass);
  858. AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
  859. dc->desc = "ASPEED 2700 Timer";
  860. awc->read = aspeed_2700_timer_read;
  861. awc->write = aspeed_2700_timer_write;
  862. }
  863. static const TypeInfo aspeed_2700_timer_info = {
  864. .name = TYPE_ASPEED_2700_TIMER,
  865. .parent = TYPE_ASPEED_TIMER,
  866. .class_init = aspeed_2700_timer_class_init,
  867. };
  868. static void aspeed_timer_register_types(void)
  869. {
  870. type_register_static(&aspeed_timer_info);
  871. type_register_static(&aspeed_2400_timer_info);
  872. type_register_static(&aspeed_2500_timer_info);
  873. type_register_static(&aspeed_2600_timer_info);
  874. type_register_static(&aspeed_1030_timer_info);
  875. type_register_static(&aspeed_2700_timer_info);
  876. }
  877. type_init(aspeed_timer_register_types)