xref: /qemu/include/hw/rtc/allwinner-rtc.h (revision e3a6e0da)
1 /*
2  * Allwinner Real Time Clock emulation
3  *
4  * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef HW_MISC_ALLWINNER_RTC_H
21 #define HW_MISC_ALLWINNER_RTC_H
22 
23 #include "qom/object.h"
24 #include "hw/sysbus.h"
25 
26 /**
27  * Constants
28  * @{
29  */
30 
31 /** Highest register address used by RTC device */
32 #define AW_RTC_REGS_MAXADDR     (0x200)
33 
34 /** Total number of known registers */
35 #define AW_RTC_REGS_NUM         (AW_RTC_REGS_MAXADDR / sizeof(uint32_t))
36 
37 /** @} */
38 
39 /**
40  * Object model types
41  * @{
42  */
43 
44 /** Generic Allwinner RTC device (abstract) */
45 #define TYPE_AW_RTC          "allwinner-rtc"
46 
47 /** Allwinner RTC sun4i family (A10, A12) */
48 #define TYPE_AW_RTC_SUN4I    TYPE_AW_RTC "-sun4i"
49 
50 /** Allwinner RTC sun6i family and newer (A31, H2+, H3, etc) */
51 #define TYPE_AW_RTC_SUN6I    TYPE_AW_RTC "-sun6i"
52 
53 /** Allwinner RTC sun7i family (A20) */
54 #define TYPE_AW_RTC_SUN7I    TYPE_AW_RTC "-sun7i"
55 
56 /** @} */
57 
58 /**
59  * Object model macros
60  * @{
61  */
62 
63 typedef struct AwRtcClass AwRtcClass;
64 typedef struct AwRtcState AwRtcState;
65 DECLARE_OBJ_CHECKERS(AwRtcState, AwRtcClass,
66                      AW_RTC, TYPE_AW_RTC)
67 
68 /** @} */
69 
70 /**
71  * Allwinner RTC per-object instance state.
72  */
73 struct AwRtcState {
74     /*< private >*/
75     SysBusDevice parent_obj;
76     /*< public >*/
77 
78     /**
79      * Actual year represented by the device when year counter is zero
80      *
81      * Can be overridden by the user using the corresponding 'base-year'
82      * property. The base year used by the target OS driver can vary, for
83      * example the Linux driver for sun6i uses 1970 while NetBSD uses 2000.
84      */
85     int base_year;
86 
87     /** Maps I/O registers in physical memory */
88     MemoryRegion iomem;
89 
90     /** Array of hardware registers */
91     uint32_t regs[AW_RTC_REGS_NUM];
92 
93 };
94 
95 /**
96  * Allwinner RTC class-level struct.
97  *
98  * This struct is filled by each sunxi device specific code
99  * such that the generic code can use this struct to support
100  * all devices.
101  */
102 struct AwRtcClass {
103     /*< private >*/
104     SysBusDeviceClass parent_class;
105     /*< public >*/
106 
107     /** Defines device specific register map */
108     const uint8_t *regmap;
109 
110     /** Size of the regmap in bytes */
111     size_t regmap_size;
112 
113     /**
114      * Read device specific register
115      *
116      * @offset: register offset to read
117      * @return true if register read successful, false otherwise
118      */
119     bool (*read)(AwRtcState *s, uint32_t offset);
120 
121     /**
122      * Write device specific register
123      *
124      * @offset: register offset to write
125      * @data: value to set in register
126      * @return true if register write successful, false otherwise
127      */
128     bool (*write)(AwRtcState *s, uint32_t offset, uint32_t data);
129 
130 };
131 
132 #endif /* HW_MISC_ALLWINNER_RTC_H */
133