xref: /netbsd-src/external/gpl3/gdb/dist/sim/bfin/dv-bfin_rtc.c (revision 1f4e7eb9e5e045e008f1894823a8e4e6c9f46890)
1 /* Blackfin Real Time Clock (RTC) model.
2 
3    Copyright (C) 2010-2024 Free Software Foundation, Inc.
4    Contributed by Analog Devices, Inc.
5 
6    This file is part of simulators.
7 
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.
12 
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.
17 
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/>.  */
20 
21 /* This must come before any other includes.  */
22 #include "defs.h"
23 
24 #include <time.h>
25 #include "sim-main.h"
26 #include "dv-sockser.h"
27 #include "devices.h"
28 #include "dv-bfin_rtc.h"
29 
30 /* XXX: This read-only stub setup is based on host system clock.  */
31 
32 struct bfin_rtc
33 {
34   bu32 base;
35   bu32 stat_shadow;
36 
37   /* Order after here is important -- matches hardware MMR layout.  */
38   bu32 stat;
39   bu16 BFIN_MMR_16(ictl);
40   bu16 BFIN_MMR_16(istat);
41   bu16 BFIN_MMR_16(swcnt);
42   bu32 alarm;
43   bu16 BFIN_MMR_16(pren);
44 };
45 #define mmr_base()      offsetof(struct bfin_rtc, stat)
46 #define mmr_offset(mmr) (offsetof(struct bfin_rtc, mmr) - mmr_base())
47 
48 static const char * const mmr_names[] =
49 {
50   "RTC_STAT", "RTC_ICTL", "RTC_ISTAT", "RTC_SWCNT", "RTC_ALARM", "RTC_PREN",
51 };
52 #define mmr_name(off) mmr_names[(off) / 4]
53 
54 static unsigned
55 bfin_rtc_io_write_buffer (struct hw *me, const void *source,
56 			  int space, address_word addr, unsigned nr_bytes)
57 {
58   struct bfin_rtc *rtc = hw_data (me);
59   bu32 mmr_off;
60   bu32 value;
61   bu16 *value16p;
62   void *valuep;
63 
64   /* Invalid access mode is higher priority than missing register.  */
65   if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, true))
66     return 0;
67 
68   if (nr_bytes == 4)
69     value = dv_load_4 (source);
70   else
71     value = dv_load_2 (source);
72 
73   mmr_off = addr - rtc->base;
74   valuep = (void *)((uintptr_t)rtc + mmr_base() + mmr_off);
75   value16p = valuep;
76 
77   HW_TRACE_WRITE ();
78 
79   /* XXX: These probably need more work.  */
80   switch (mmr_off)
81     {
82     case mmr_offset(stat):
83       /* XXX: Ignore these since we are wired to host.  */
84       break;
85     case mmr_offset(istat):
86       dv_w1c_2 (value16p, value, ~(1 << 14));
87       break;
88     case mmr_offset(alarm):
89       break;
90     case mmr_offset(ictl):
91       /* XXX: This should schedule an event handler.  */
92     case mmr_offset(swcnt):
93     case mmr_offset(pren):
94       break;
95     }
96 
97   return nr_bytes;
98 }
99 
100 static unsigned
101 bfin_rtc_io_read_buffer (struct hw *me, void *dest,
102 			 int space, address_word addr, unsigned nr_bytes)
103 {
104   struct bfin_rtc *rtc = hw_data (me);
105   bu32 mmr_off;
106   bu16 *value16p;
107   bu32 *value32p;
108   void *valuep;
109 
110   /* Invalid access mode is higher priority than missing register.  */
111   if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, false))
112     return 0;
113 
114   mmr_off = addr - rtc->base;
115   valuep = (void *)((uintptr_t)rtc + mmr_base() + mmr_off);
116   value16p = valuep;
117   value32p = valuep;
118 
119   HW_TRACE_READ ();
120 
121   switch (mmr_off)
122     {
123     case mmr_offset(stat):
124       {
125 	time_t t = time (NULL);
126 	struct tm *tm = localtime (&t);
127 	bu32 value =
128 	  (((tm->tm_year - 70) * 365 + tm->tm_yday) << 17) |
129 	  (tm->tm_hour << 12) |
130 	  (tm->tm_min << 6) |
131 	  (tm->tm_sec << 0);
132 	dv_store_4 (dest, value);
133 	break;
134       }
135     case mmr_offset(alarm):
136       dv_store_4 (dest, *value32p);
137       break;
138     case mmr_offset(istat):
139     case mmr_offset(ictl):
140     case mmr_offset(swcnt):
141     case mmr_offset(pren):
142       dv_store_2 (dest, *value16p);
143       break;
144     }
145 
146   return nr_bytes;
147 }
148 
149 static const struct hw_port_descriptor bfin_rtc_ports[] =
150 {
151   { "rtc", 0, 0, output_port, },
152   { NULL, 0, 0, 0, },
153 };
154 
155 static void
156 attach_bfin_rtc_regs (struct hw *me, struct bfin_rtc *rtc)
157 {
158   address_word attach_address;
159   int attach_space;
160   unsigned attach_size;
161   reg_property_spec reg;
162 
163   if (hw_find_property (me, "reg") == NULL)
164     hw_abort (me, "Missing \"reg\" property");
165 
166   if (!hw_find_reg_array_property (me, "reg", 0, &reg))
167     hw_abort (me, "\"reg\" property must contain three addr/size entries");
168 
169   hw_unit_address_to_attach_address (hw_parent (me),
170 				     &reg.address,
171 				     &attach_space, &attach_address, me);
172   hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
173 
174   if (attach_size != BFIN_MMR_RTC_SIZE)
175     hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_RTC_SIZE);
176 
177   hw_attach_address (hw_parent (me),
178 		     0, attach_space, attach_address, attach_size, me);
179 
180   rtc->base = attach_address;
181 }
182 
183 static void
184 bfin_rtc_finish (struct hw *me)
185 {
186   struct bfin_rtc *rtc;
187 
188   rtc = HW_ZALLOC (me, struct bfin_rtc);
189 
190   set_hw_data (me, rtc);
191   set_hw_io_read_buffer (me, bfin_rtc_io_read_buffer);
192   set_hw_io_write_buffer (me, bfin_rtc_io_write_buffer);
193   set_hw_ports (me, bfin_rtc_ports);
194 
195   attach_bfin_rtc_regs (me, rtc);
196 
197   /* Initialize the RTC.  */
198 }
199 
200 const struct hw_descriptor dv_bfin_rtc_descriptor[] =
201 {
202   {"bfin_rtc", bfin_rtc_finish,},
203   {NULL, NULL},
204 };
205