xref: /qemu/include/hw/timer/avr_timer16.h (revision 8ff47bc1a0c798141479530bf9cb3836b49fc5e1)
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  * On 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 #ifndef HW_TIMER_AVR_TIMER16_H
29*8ff47bc1SMichael Rolnik #define HW_TIMER_AVR_TIMER16_H
30*8ff47bc1SMichael Rolnik 
31*8ff47bc1SMichael Rolnik #include "hw/sysbus.h"
32*8ff47bc1SMichael Rolnik #include "qemu/timer.h"
33*8ff47bc1SMichael Rolnik #include "hw/hw.h"
34*8ff47bc1SMichael Rolnik 
35*8ff47bc1SMichael Rolnik enum NextInterrupt {
36*8ff47bc1SMichael Rolnik     OVERFLOW,
37*8ff47bc1SMichael Rolnik     COMPA,
38*8ff47bc1SMichael Rolnik     COMPB,
39*8ff47bc1SMichael Rolnik     COMPC,
40*8ff47bc1SMichael Rolnik     CAPT
41*8ff47bc1SMichael Rolnik };
42*8ff47bc1SMichael Rolnik 
43*8ff47bc1SMichael Rolnik #define TYPE_AVR_TIMER16 "avr-timer16"
44*8ff47bc1SMichael Rolnik #define AVR_TIMER16(obj) \
45*8ff47bc1SMichael Rolnik     OBJECT_CHECK(AVRTimer16State, (obj), TYPE_AVR_TIMER16)
46*8ff47bc1SMichael Rolnik 
47*8ff47bc1SMichael Rolnik typedef struct AVRTimer16State {
48*8ff47bc1SMichael Rolnik     /* <private> */
49*8ff47bc1SMichael Rolnik     SysBusDevice parent_obj;
50*8ff47bc1SMichael Rolnik 
51*8ff47bc1SMichael Rolnik     /* <public> */
52*8ff47bc1SMichael Rolnik     MemoryRegion iomem;
53*8ff47bc1SMichael Rolnik     MemoryRegion imsk_iomem;
54*8ff47bc1SMichael Rolnik     MemoryRegion ifr_iomem;
55*8ff47bc1SMichael Rolnik     QEMUTimer *timer;
56*8ff47bc1SMichael Rolnik     qemu_irq capt_irq;
57*8ff47bc1SMichael Rolnik     qemu_irq compa_irq;
58*8ff47bc1SMichael Rolnik     qemu_irq compb_irq;
59*8ff47bc1SMichael Rolnik     qemu_irq compc_irq;
60*8ff47bc1SMichael Rolnik     qemu_irq ovf_irq;
61*8ff47bc1SMichael Rolnik 
62*8ff47bc1SMichael Rolnik     bool enabled;
63*8ff47bc1SMichael Rolnik 
64*8ff47bc1SMichael Rolnik     /* registers */
65*8ff47bc1SMichael Rolnik     uint8_t cra;
66*8ff47bc1SMichael Rolnik     uint8_t crb;
67*8ff47bc1SMichael Rolnik     uint8_t crc;
68*8ff47bc1SMichael Rolnik     uint8_t cntl;
69*8ff47bc1SMichael Rolnik     uint8_t cnth;
70*8ff47bc1SMichael Rolnik     uint8_t icrl;
71*8ff47bc1SMichael Rolnik     uint8_t icrh;
72*8ff47bc1SMichael Rolnik     uint8_t ocral;
73*8ff47bc1SMichael Rolnik     uint8_t ocrah;
74*8ff47bc1SMichael Rolnik     uint8_t ocrbl;
75*8ff47bc1SMichael Rolnik     uint8_t ocrbh;
76*8ff47bc1SMichael Rolnik     uint8_t ocrcl;
77*8ff47bc1SMichael Rolnik     uint8_t ocrch;
78*8ff47bc1SMichael Rolnik     /*
79*8ff47bc1SMichael Rolnik      * Reads and writes to CNT and ICR utilise a bizarre temporary
80*8ff47bc1SMichael Rolnik      * register, which we emulate
81*8ff47bc1SMichael Rolnik      */
82*8ff47bc1SMichael Rolnik     uint8_t rtmp;
83*8ff47bc1SMichael Rolnik     uint8_t imsk;
84*8ff47bc1SMichael Rolnik     uint8_t ifr;
85*8ff47bc1SMichael Rolnik 
86*8ff47bc1SMichael Rolnik     uint8_t id;
87*8ff47bc1SMichael Rolnik     uint64_t cpu_freq_hz;
88*8ff47bc1SMichael Rolnik     uint64_t freq_hz;
89*8ff47bc1SMichael Rolnik     uint64_t period_ns;
90*8ff47bc1SMichael Rolnik     uint64_t reset_time_ns;
91*8ff47bc1SMichael Rolnik     enum NextInterrupt next_interrupt;
92*8ff47bc1SMichael Rolnik } AVRTimer16State;
93*8ff47bc1SMichael Rolnik 
94*8ff47bc1SMichael Rolnik #endif /* HW_TIMER_AVR_TIMER16_H */
95