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