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