1 /* Blackfin Real Time Clock (RTC) model.
3 Copyright (C) 2010-2021 Free Software Foundation, Inc.
4 Contributed by Analog Devices, Inc.
6 This file is part of simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* This must come before any other includes. */
26 #include "dv-sockser.h"
28 #include "dv-bfin_rtc.h"
30 /* XXX: This read-only stub setup is based on host system clock. */
37 /* Order after here is important -- matches hardware MMR layout. */
39 bu16
BFIN_MMR_16(ictl
);
40 bu16
BFIN_MMR_16(istat
);
41 bu16
BFIN_MMR_16(swcnt
);
43 bu16
BFIN_MMR_16(pren
);
45 #define mmr_base() offsetof(struct bfin_rtc, stat)
46 #define mmr_offset(mmr) (offsetof(struct bfin_rtc, mmr) - mmr_base())
48 static const char * const mmr_names
[] =
50 "RTC_STAT", "RTC_ICTL", "RTC_ISTAT", "RTC_SWCNT", "RTC_ALARM", "RTC_PREN",
52 #define mmr_name(off) mmr_names[(off) / 4]
55 bfin_rtc_io_write_buffer (struct hw
*me
, const void *source
,
56 int space
, address_word addr
, unsigned nr_bytes
)
58 struct bfin_rtc
*rtc
= hw_data (me
);
65 /* Invalid access mode is higher priority than missing register. */
66 if (!dv_bfin_mmr_require_16_32 (me
, addr
, nr_bytes
, true))
70 value
= dv_load_4 (source
);
72 value
= dv_load_2 (source
);
74 mmr_off
= addr
- rtc
->base
;
75 valuep
= (void *)((unsigned long)rtc
+ mmr_base() + mmr_off
);
81 /* XXX: These probably need more work. */
84 case mmr_offset(stat
):
85 /* XXX: Ignore these since we are wired to host. */
87 case mmr_offset(istat
):
88 dv_w1c_2 (value16p
, value
, ~(1 << 14));
90 case mmr_offset(alarm
):
92 case mmr_offset(ictl
):
93 /* XXX: This should schedule an event handler. */
94 case mmr_offset(swcnt
):
95 case mmr_offset(pren
):
103 bfin_rtc_io_read_buffer (struct hw
*me
, void *dest
,
104 int space
, address_word addr
, unsigned nr_bytes
)
106 struct bfin_rtc
*rtc
= hw_data (me
);
112 /* Invalid access mode is higher priority than missing register. */
113 if (!dv_bfin_mmr_require_16_32 (me
, addr
, nr_bytes
, false))
116 mmr_off
= addr
- rtc
->base
;
117 valuep
= (void *)((unsigned long)rtc
+ mmr_base() + mmr_off
);
125 case mmr_offset(stat
):
127 time_t t
= time (NULL
);
128 struct tm
*tm
= localtime (&t
);
130 (((tm
->tm_year
- 70) * 365 + tm
->tm_yday
) << 17) |
131 (tm
->tm_hour
<< 12) |
134 dv_store_4 (dest
, value
);
137 case mmr_offset(alarm
):
138 dv_store_4 (dest
, *value32p
);
140 case mmr_offset(istat
):
141 case mmr_offset(ictl
):
142 case mmr_offset(swcnt
):
143 case mmr_offset(pren
):
144 dv_store_2 (dest
, *value16p
);
151 static const struct hw_port_descriptor bfin_rtc_ports
[] =
153 { "rtc", 0, 0, output_port
, },
158 attach_bfin_rtc_regs (struct hw
*me
, struct bfin_rtc
*rtc
)
160 address_word attach_address
;
162 unsigned attach_size
;
163 reg_property_spec reg
;
165 if (hw_find_property (me
, "reg") == NULL
)
166 hw_abort (me
, "Missing \"reg\" property");
168 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
169 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
171 hw_unit_address_to_attach_address (hw_parent (me
),
173 &attach_space
, &attach_address
, me
);
174 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
176 if (attach_size
!= BFIN_MMR_RTC_SIZE
)
177 hw_abort (me
, "\"reg\" size must be %#x", BFIN_MMR_RTC_SIZE
);
179 hw_attach_address (hw_parent (me
),
180 0, attach_space
, attach_address
, attach_size
, me
);
182 rtc
->base
= attach_address
;
186 bfin_rtc_finish (struct hw
*me
)
188 struct bfin_rtc
*rtc
;
190 rtc
= HW_ZALLOC (me
, struct bfin_rtc
);
192 set_hw_data (me
, rtc
);
193 set_hw_io_read_buffer (me
, bfin_rtc_io_read_buffer
);
194 set_hw_io_write_buffer (me
, bfin_rtc_io_write_buffer
);
195 set_hw_ports (me
, bfin_rtc_ports
);
197 attach_bfin_rtc_regs (me
, rtc
);
199 /* Initialize the RTC. */
202 const struct hw_descriptor dv_bfin_rtc_descriptor
[] =
204 {"bfin_rtc", bfin_rtc_finish
,},