avr_timer16.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * AVR 16-bit timer
  3. *
  4. * Copyright (c) 2018 University of Kent
  5. * Author: Ed Robbins
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see
  19. * <http://www.gnu.org/licenses/lgpl-2.1.html>
  20. */
  21. /*
  22. * Driver for 16 bit timers on 8 bit AVR devices.
  23. * Note:
  24. * ATmega640/V-1280/V-1281/V-2560/V-2561/V timers 1, 3, 4 and 5 are 16 bit
  25. */
  26. /*
  27. * XXX TODO: Power Reduction Register support
  28. * prescaler pause support
  29. * PWM modes, GPIO, output capture pins, input compare pin
  30. */
  31. #include "qemu/osdep.h"
  32. #include "qapi/error.h"
  33. #include "qemu/log.h"
  34. #include "hw/irq.h"
  35. #include "hw/qdev-properties.h"
  36. #include "hw/timer/avr_timer16.h"
  37. #include "trace.h"
  38. /* Register offsets */
  39. #define T16_CRA 0x0
  40. #define T16_CRB 0x1
  41. #define T16_CRC 0x2
  42. #define T16_CNTL 0x4
  43. #define T16_CNTH 0x5
  44. #define T16_ICRL 0x6
  45. #define T16_ICRH 0x7
  46. #define T16_OCRAL 0x8
  47. #define T16_OCRAH 0x9
  48. #define T16_OCRBL 0xa
  49. #define T16_OCRBH 0xb
  50. #define T16_OCRCL 0xc
  51. #define T16_OCRCH 0xd
  52. /* Field masks */
  53. #define T16_CRA_WGM01 0x3
  54. #define T16_CRA_COMC 0xc
  55. #define T16_CRA_COMB 0x30
  56. #define T16_CRA_COMA 0xc0
  57. #define T16_CRA_OC_CONF \
  58. (T16_CRA_COMA | T16_CRA_COMB | T16_CRA_COMC)
  59. #define T16_CRB_CS 0x7
  60. #define T16_CRB_WGM23 0x18
  61. #define T16_CRB_ICES 0x40
  62. #define T16_CRB_ICNC 0x80
  63. #define T16_CRC_FOCC 0x20
  64. #define T16_CRC_FOCB 0x40
  65. #define T16_CRC_FOCA 0x80
  66. /* Fields masks both TIMSK and TIFR (interrupt mask/flag registers) */
  67. #define T16_INT_TOV 0x1 /* Timer overflow */
  68. #define T16_INT_OCA 0x2 /* Output compare A */
  69. #define T16_INT_OCB 0x4 /* Output compare B */
  70. #define T16_INT_OCC 0x8 /* Output compare C */
  71. #define T16_INT_IC 0x20 /* Input capture */
  72. /* Clock source values */
  73. #define T16_CLKSRC_STOPPED 0
  74. #define T16_CLKSRC_DIV1 1
  75. #define T16_CLKSRC_DIV8 2
  76. #define T16_CLKSRC_DIV64 3
  77. #define T16_CLKSRC_DIV256 4
  78. #define T16_CLKSRC_DIV1024 5
  79. #define T16_CLKSRC_EXT_FALLING 6
  80. #define T16_CLKSRC_EXT_RISING 7
  81. /* Timer mode values (not including PWM modes) */
  82. #define T16_MODE_NORMAL 0
  83. #define T16_MODE_CTC_OCRA 4
  84. #define T16_MODE_CTC_ICR 12
  85. /* Accessors */
  86. #define CLKSRC(t16) (t16->crb & T16_CRB_CS)
  87. #define MODE(t16) (((t16->crb & T16_CRB_WGM23) >> 1) | \
  88. (t16->cra & T16_CRA_WGM01))
  89. #define CNT(t16) VAL16(t16->cntl, t16->cnth)
  90. #define OCRA(t16) VAL16(t16->ocral, t16->ocrah)
  91. #define OCRB(t16) VAL16(t16->ocrbl, t16->ocrbh)
  92. #define OCRC(t16) VAL16(t16->ocrcl, t16->ocrch)
  93. #define ICR(t16) VAL16(t16->icrl, t16->icrh)
  94. /* Helper macros */
  95. #define VAL16(l, h) ((h << 8) | l)
  96. #define DB_PRINT(fmt, args...) /* Nothing */
  97. static inline int64_t avr_timer16_ns_to_ticks(AVRTimer16State *t16, int64_t t)
  98. {
  99. if (t16->period_ns == 0) {
  100. return 0;
  101. }
  102. return t / t16->period_ns;
  103. }
  104. static void avr_timer16_update_cnt(AVRTimer16State *t16)
  105. {
  106. uint16_t cnt;
  107. cnt = avr_timer16_ns_to_ticks(t16, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) -
  108. t16->reset_time_ns);
  109. t16->cntl = (uint8_t)(cnt & 0xff);
  110. t16->cnth = (uint8_t)((cnt & 0xff00) >> 8);
  111. }
  112. static inline void avr_timer16_recalc_reset_time(AVRTimer16State *t16)
  113. {
  114. t16->reset_time_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) -
  115. CNT(t16) * t16->period_ns;
  116. }
  117. static void avr_timer16_clock_reset(AVRTimer16State *t16)
  118. {
  119. t16->cntl = 0;
  120. t16->cnth = 0;
  121. t16->reset_time_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  122. }
  123. static void avr_timer16_clksrc_update(AVRTimer16State *t16)
  124. {
  125. uint16_t divider = 0;
  126. switch (CLKSRC(t16)) {
  127. case T16_CLKSRC_EXT_FALLING:
  128. case T16_CLKSRC_EXT_RISING:
  129. qemu_log_mask(LOG_UNIMP, "%s: external clock source unsupported\n",
  130. __func__);
  131. break;
  132. case T16_CLKSRC_STOPPED:
  133. break;
  134. case T16_CLKSRC_DIV1:
  135. divider = 1;
  136. break;
  137. case T16_CLKSRC_DIV8:
  138. divider = 8;
  139. break;
  140. case T16_CLKSRC_DIV64:
  141. divider = 64;
  142. break;
  143. case T16_CLKSRC_DIV256:
  144. divider = 256;
  145. break;
  146. case T16_CLKSRC_DIV1024:
  147. divider = 1024;
  148. break;
  149. default:
  150. break;
  151. }
  152. if (divider) {
  153. t16->freq_hz = t16->cpu_freq_hz / divider;
  154. t16->period_ns = NANOSECONDS_PER_SECOND / t16->freq_hz;
  155. trace_avr_timer16_clksrc_update(t16->freq_hz, t16->period_ns,
  156. (uint64_t)(1e6 / t16->freq_hz));
  157. }
  158. }
  159. static void avr_timer16_set_alarm(AVRTimer16State *t16)
  160. {
  161. if (CLKSRC(t16) == T16_CLKSRC_EXT_FALLING ||
  162. CLKSRC(t16) == T16_CLKSRC_EXT_RISING ||
  163. CLKSRC(t16) == T16_CLKSRC_STOPPED) {
  164. /* Timer is disabled or set to external clock source (unsupported) */
  165. return;
  166. }
  167. uint64_t alarm_offset = 0xffff;
  168. enum NextInterrupt next_interrupt = OVERFLOW;
  169. switch (MODE(t16)) {
  170. case T16_MODE_NORMAL:
  171. /* Normal mode */
  172. if (OCRA(t16) < alarm_offset && OCRA(t16) > CNT(t16) &&
  173. (t16->imsk & T16_INT_OCA)) {
  174. alarm_offset = OCRA(t16);
  175. next_interrupt = COMPA;
  176. }
  177. break;
  178. case T16_MODE_CTC_OCRA:
  179. /* CTC mode, top = ocra */
  180. if (OCRA(t16) < alarm_offset && OCRA(t16) > CNT(t16)) {
  181. alarm_offset = OCRA(t16);
  182. next_interrupt = COMPA;
  183. }
  184. break;
  185. case T16_MODE_CTC_ICR:
  186. /* CTC mode, top = icr */
  187. if (ICR(t16) < alarm_offset && ICR(t16) > CNT(t16)) {
  188. alarm_offset = ICR(t16);
  189. next_interrupt = CAPT;
  190. }
  191. if (OCRA(t16) < alarm_offset && OCRA(t16) > CNT(t16) &&
  192. (t16->imsk & T16_INT_OCA)) {
  193. alarm_offset = OCRA(t16);
  194. next_interrupt = COMPA;
  195. }
  196. break;
  197. default:
  198. qemu_log_mask(LOG_UNIMP, "%s: pwm modes are unsupported\n",
  199. __func__);
  200. return;
  201. }
  202. if (OCRB(t16) < alarm_offset && OCRB(t16) > CNT(t16) &&
  203. (t16->imsk & T16_INT_OCB)) {
  204. alarm_offset = OCRB(t16);
  205. next_interrupt = COMPB;
  206. }
  207. if (OCRC(t16) < alarm_offset && OCRB(t16) > CNT(t16) &&
  208. (t16->imsk & T16_INT_OCC)) {
  209. alarm_offset = OCRB(t16);
  210. next_interrupt = COMPC;
  211. }
  212. alarm_offset -= CNT(t16);
  213. t16->next_interrupt = next_interrupt;
  214. uint64_t alarm_ns =
  215. t16->reset_time_ns + ((CNT(t16) + alarm_offset) * t16->period_ns);
  216. timer_mod(t16->timer, alarm_ns);
  217. trace_avr_timer16_next_alarm(alarm_offset * t16->period_ns);
  218. }
  219. static void avr_timer16_interrupt(void *opaque)
  220. {
  221. AVRTimer16State *t16 = opaque;
  222. uint8_t mode = MODE(t16);
  223. avr_timer16_update_cnt(t16);
  224. if (CLKSRC(t16) == T16_CLKSRC_EXT_FALLING ||
  225. CLKSRC(t16) == T16_CLKSRC_EXT_RISING ||
  226. CLKSRC(t16) == T16_CLKSRC_STOPPED) {
  227. /* Timer is disabled or set to external clock source (unsupported) */
  228. return;
  229. }
  230. trace_avr_timer16_interrupt_count(CNT(t16));
  231. /* Counter overflow */
  232. if (t16->next_interrupt == OVERFLOW) {
  233. trace_avr_timer16_interrupt_overflow("counter 0xffff");
  234. avr_timer16_clock_reset(t16);
  235. if (t16->imsk & T16_INT_TOV) {
  236. t16->ifr |= T16_INT_TOV;
  237. qemu_set_irq(t16->ovf_irq, 1);
  238. }
  239. }
  240. /* Check for ocra overflow in CTC mode */
  241. if (mode == T16_MODE_CTC_OCRA && t16->next_interrupt == COMPA) {
  242. trace_avr_timer16_interrupt_overflow("CTC OCRA");
  243. avr_timer16_clock_reset(t16);
  244. }
  245. /* Check for icr overflow in CTC mode */
  246. if (mode == T16_MODE_CTC_ICR && t16->next_interrupt == CAPT) {
  247. trace_avr_timer16_interrupt_overflow("CTC ICR");
  248. avr_timer16_clock_reset(t16);
  249. if (t16->imsk & T16_INT_IC) {
  250. t16->ifr |= T16_INT_IC;
  251. qemu_set_irq(t16->capt_irq, 1);
  252. }
  253. }
  254. /* Check for output compare interrupts */
  255. if (t16->imsk & T16_INT_OCA && t16->next_interrupt == COMPA) {
  256. t16->ifr |= T16_INT_OCA;
  257. qemu_set_irq(t16->compa_irq, 1);
  258. }
  259. if (t16->imsk & T16_INT_OCB && t16->next_interrupt == COMPB) {
  260. t16->ifr |= T16_INT_OCB;
  261. qemu_set_irq(t16->compb_irq, 1);
  262. }
  263. if (t16->imsk & T16_INT_OCC && t16->next_interrupt == COMPC) {
  264. t16->ifr |= T16_INT_OCC;
  265. qemu_set_irq(t16->compc_irq, 1);
  266. }
  267. avr_timer16_set_alarm(t16);
  268. }
  269. static void avr_timer16_reset(DeviceState *dev)
  270. {
  271. AVRTimer16State *t16 = AVR_TIMER16(dev);
  272. avr_timer16_clock_reset(t16);
  273. avr_timer16_clksrc_update(t16);
  274. avr_timer16_set_alarm(t16);
  275. qemu_set_irq(t16->capt_irq, 0);
  276. qemu_set_irq(t16->compa_irq, 0);
  277. qemu_set_irq(t16->compb_irq, 0);
  278. qemu_set_irq(t16->compc_irq, 0);
  279. qemu_set_irq(t16->ovf_irq, 0);
  280. }
  281. static uint64_t avr_timer16_read(void *opaque, hwaddr offset, unsigned size)
  282. {
  283. assert(size == 1);
  284. AVRTimer16State *t16 = opaque;
  285. uint8_t retval = 0;
  286. switch (offset) {
  287. case T16_CRA:
  288. retval = t16->cra;
  289. break;
  290. case T16_CRB:
  291. retval = t16->crb;
  292. break;
  293. case T16_CRC:
  294. retval = t16->crc;
  295. break;
  296. case T16_CNTL:
  297. avr_timer16_update_cnt(t16);
  298. t16->rtmp = t16->cnth;
  299. retval = t16->cntl;
  300. break;
  301. case T16_CNTH:
  302. retval = t16->rtmp;
  303. break;
  304. case T16_ICRL:
  305. /*
  306. * The timer copies cnt to icr when the input capture pin changes
  307. * state or when the analog comparator has a match. We don't
  308. * emulate this behaviour. We do support it's use for defining a
  309. * TOP value in T16_MODE_CTC_ICR
  310. */
  311. t16->rtmp = t16->icrh;
  312. retval = t16->icrl;
  313. break;
  314. case T16_ICRH:
  315. retval = t16->rtmp;
  316. break;
  317. case T16_OCRAL:
  318. retval = t16->ocral;
  319. break;
  320. case T16_OCRAH:
  321. retval = t16->ocrah;
  322. break;
  323. case T16_OCRBL:
  324. retval = t16->ocrbl;
  325. break;
  326. case T16_OCRBH:
  327. retval = t16->ocrbh;
  328. break;
  329. case T16_OCRCL:
  330. retval = t16->ocrcl;
  331. break;
  332. case T16_OCRCH:
  333. retval = t16->ocrch;
  334. break;
  335. default:
  336. break;
  337. }
  338. trace_avr_timer16_read(offset, retval);
  339. return (uint64_t)retval;
  340. }
  341. static void avr_timer16_write(void *opaque, hwaddr offset,
  342. uint64_t val64, unsigned size)
  343. {
  344. assert(size == 1);
  345. AVRTimer16State *t16 = opaque;
  346. uint8_t val8 = (uint8_t)val64;
  347. uint8_t prev_clk_src = CLKSRC(t16);
  348. trace_avr_timer16_write(offset, val8);
  349. switch (offset) {
  350. case T16_CRA:
  351. t16->cra = val8;
  352. if (t16->cra & T16_CRA_OC_CONF) {
  353. qemu_log_mask(LOG_UNIMP, "%s: output compare pins unsupported\n",
  354. __func__);
  355. }
  356. break;
  357. case T16_CRB:
  358. t16->crb = val8;
  359. if (t16->crb & T16_CRB_ICNC) {
  360. qemu_log_mask(LOG_UNIMP,
  361. "%s: input capture noise canceller unsupported\n",
  362. __func__);
  363. }
  364. if (t16->crb & T16_CRB_ICES) {
  365. qemu_log_mask(LOG_UNIMP, "%s: input capture unsupported\n",
  366. __func__);
  367. }
  368. if (CLKSRC(t16) != prev_clk_src) {
  369. avr_timer16_clksrc_update(t16);
  370. if (prev_clk_src == T16_CLKSRC_STOPPED) {
  371. t16->reset_time_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  372. }
  373. }
  374. break;
  375. case T16_CRC:
  376. t16->crc = val8;
  377. qemu_log_mask(LOG_UNIMP, "%s: output compare pins unsupported\n",
  378. __func__);
  379. break;
  380. case T16_CNTL:
  381. /*
  382. * CNT is the 16-bit counter value, it must be read/written via
  383. * a temporary register (rtmp) to make the read/write atomic.
  384. */
  385. /* ICR also has this behaviour, and shares rtmp */
  386. /*
  387. * Writing CNT blocks compare matches for one clock cycle.
  388. * Writing CNT to TOP or to an OCR value (if in use) will
  389. * skip the relevant interrupt
  390. */
  391. t16->cntl = val8;
  392. t16->cnth = t16->rtmp;
  393. avr_timer16_recalc_reset_time(t16);
  394. break;
  395. case T16_CNTH:
  396. t16->rtmp = val8;
  397. break;
  398. case T16_ICRL:
  399. /* ICR can only be written in mode T16_MODE_CTC_ICR */
  400. if (MODE(t16) == T16_MODE_CTC_ICR) {
  401. t16->icrl = val8;
  402. t16->icrh = t16->rtmp;
  403. }
  404. break;
  405. case T16_ICRH:
  406. if (MODE(t16) == T16_MODE_CTC_ICR) {
  407. t16->rtmp = val8;
  408. }
  409. break;
  410. case T16_OCRAL:
  411. /*
  412. * OCRn cause the relevant output compare flag to be raised, and
  413. * trigger an interrupt, when CNT is equal to the value here
  414. */
  415. t16->ocral = val8;
  416. break;
  417. case T16_OCRAH:
  418. t16->ocrah = val8;
  419. break;
  420. case T16_OCRBL:
  421. t16->ocrbl = val8;
  422. break;
  423. case T16_OCRBH:
  424. t16->ocrbh = val8;
  425. break;
  426. case T16_OCRCL:
  427. t16->ocrcl = val8;
  428. break;
  429. case T16_OCRCH:
  430. t16->ocrch = val8;
  431. break;
  432. default:
  433. break;
  434. }
  435. avr_timer16_set_alarm(t16);
  436. }
  437. static uint64_t avr_timer16_imsk_read(void *opaque,
  438. hwaddr offset,
  439. unsigned size)
  440. {
  441. assert(size == 1);
  442. AVRTimer16State *t16 = opaque;
  443. trace_avr_timer16_read_imsk(offset ? 0 : t16->imsk);
  444. if (offset != 0) {
  445. return 0;
  446. }
  447. return t16->imsk;
  448. }
  449. static void avr_timer16_imsk_write(void *opaque, hwaddr offset,
  450. uint64_t val64, unsigned size)
  451. {
  452. assert(size == 1);
  453. AVRTimer16State *t16 = opaque;
  454. trace_avr_timer16_write_imsk(val64);
  455. if (offset != 0) {
  456. return;
  457. }
  458. t16->imsk = (uint8_t)val64;
  459. }
  460. static uint64_t avr_timer16_ifr_read(void *opaque,
  461. hwaddr offset,
  462. unsigned size)
  463. {
  464. assert(size == 1);
  465. AVRTimer16State *t16 = opaque;
  466. trace_avr_timer16_read_ifr(offset ? 0 : t16->ifr);
  467. if (offset != 0) {
  468. return 0;
  469. }
  470. return t16->ifr;
  471. }
  472. static void avr_timer16_ifr_write(void *opaque, hwaddr offset,
  473. uint64_t val64, unsigned size)
  474. {
  475. assert(size == 1);
  476. AVRTimer16State *t16 = opaque;
  477. trace_avr_timer16_write_imsk(val64);
  478. if (offset != 0) {
  479. return;
  480. }
  481. t16->ifr = (uint8_t)val64;
  482. }
  483. static const MemoryRegionOps avr_timer16_ops = {
  484. .read = avr_timer16_read,
  485. .write = avr_timer16_write,
  486. .endianness = DEVICE_NATIVE_ENDIAN,
  487. .impl = {.max_access_size = 1}
  488. };
  489. static const MemoryRegionOps avr_timer16_imsk_ops = {
  490. .read = avr_timer16_imsk_read,
  491. .write = avr_timer16_imsk_write,
  492. .endianness = DEVICE_NATIVE_ENDIAN,
  493. .impl = {.max_access_size = 1}
  494. };
  495. static const MemoryRegionOps avr_timer16_ifr_ops = {
  496. .read = avr_timer16_ifr_read,
  497. .write = avr_timer16_ifr_write,
  498. .endianness = DEVICE_NATIVE_ENDIAN,
  499. .impl = {.max_access_size = 1}
  500. };
  501. static Property avr_timer16_properties[] = {
  502. DEFINE_PROP_UINT8("id", struct AVRTimer16State, id, 0),
  503. DEFINE_PROP_UINT64("cpu-frequency-hz", struct AVRTimer16State,
  504. cpu_freq_hz, 0),
  505. DEFINE_PROP_END_OF_LIST(),
  506. };
  507. static void avr_timer16_pr(void *opaque, int irq, int level)
  508. {
  509. AVRTimer16State *s = AVR_TIMER16(opaque);
  510. s->enabled = !level;
  511. if (!s->enabled) {
  512. avr_timer16_reset(DEVICE(s));
  513. }
  514. }
  515. static void avr_timer16_init(Object *obj)
  516. {
  517. AVRTimer16State *s = AVR_TIMER16(obj);
  518. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->capt_irq);
  519. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->compa_irq);
  520. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->compb_irq);
  521. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->compc_irq);
  522. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->ovf_irq);
  523. memory_region_init_io(&s->iomem, obj, &avr_timer16_ops,
  524. s, "avr-timer16", 0xe);
  525. memory_region_init_io(&s->imsk_iomem, obj, &avr_timer16_imsk_ops,
  526. s, "avr-timer16-intmask", 0x1);
  527. memory_region_init_io(&s->ifr_iomem, obj, &avr_timer16_ifr_ops,
  528. s, "avr-timer16-intflag", 0x1);
  529. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
  530. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->imsk_iomem);
  531. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->ifr_iomem);
  532. qdev_init_gpio_in(DEVICE(s), avr_timer16_pr, 1);
  533. }
  534. static void avr_timer16_realize(DeviceState *dev, Error **errp)
  535. {
  536. AVRTimer16State *s = AVR_TIMER16(dev);
  537. if (s->cpu_freq_hz == 0) {
  538. error_setg(errp, "AVR timer16: cpu-frequency-hz property must be set");
  539. return;
  540. }
  541. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, avr_timer16_interrupt, s);
  542. s->enabled = true;
  543. }
  544. static void avr_timer16_class_init(ObjectClass *klass, void *data)
  545. {
  546. DeviceClass *dc = DEVICE_CLASS(klass);
  547. dc->reset = avr_timer16_reset;
  548. dc->realize = avr_timer16_realize;
  549. device_class_set_props(dc, avr_timer16_properties);
  550. }
  551. static const TypeInfo avr_timer16_info = {
  552. .name = TYPE_AVR_TIMER16,
  553. .parent = TYPE_SYS_BUS_DEVICE,
  554. .instance_size = sizeof(AVRTimer16State),
  555. .instance_init = avr_timer16_init,
  556. .class_init = avr_timer16_class_init,
  557. };
  558. static void avr_timer16_register_types(void)
  559. {
  560. type_register_static(&avr_timer16_info);
  561. }
  562. type_init(avr_timer16_register_types)