sim: bfin: new port
[deliverable/binutils-gdb.git] / sim / bfin / dv-bfin_rtc.c
1 /* Blackfin Real Time Clock (RTC) model.
2
3 Copyright (C) 2010-2011 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 "RTC_STAT", "RTC_ICTL", "RTC_ISTAT", "RTC_SWCNT", "RTC_ALARM", "RTC_PREN",
49 };
50 #define mmr_name(off) mmr_names[(off) / 4]
51
52 static unsigned
53 bfin_rtc_io_write_buffer (struct hw *me, const void *source,
54 int space, address_word addr, unsigned nr_bytes)
55 {
56 struct bfin_rtc *rtc = hw_data (me);
57 bu32 mmr_off;
58 bu32 value;
59 bu16 *value16p;
60 bu32 *value32p;
61 void *valuep;
62
63 if (nr_bytes == 4)
64 value = dv_load_4 (source);
65 else
66 value = dv_load_2 (source);
67
68 mmr_off = addr - rtc->base;
69 valuep = (void *)((unsigned long)rtc + mmr_base() + mmr_off);
70 value16p = valuep;
71 value32p = valuep;
72
73 HW_TRACE_WRITE ();
74
75 /* XXX: These probably need more work. */
76 switch (mmr_off)
77 {
78 case mmr_offset(stat):
79 /* XXX: Ignore these since we are wired to host. */
80 break;
81 case mmr_offset(istat):
82 dv_w1c_2 (value16p, value, 1 << 14);
83 break;
84 case mmr_offset(alarm):
85 break;
86 case mmr_offset(ictl):
87 /* XXX: This should schedule an event handler. */
88 case mmr_offset(swcnt):
89 case mmr_offset(pren):
90 break;
91 }
92
93 return nr_bytes;
94 }
95
96 static unsigned
97 bfin_rtc_io_read_buffer (struct hw *me, void *dest,
98 int space, address_word addr, unsigned nr_bytes)
99 {
100 struct bfin_rtc *rtc = hw_data (me);
101 bu32 mmr_off;
102 bu16 *value16p;
103 bu32 *value32p;
104 void *valuep;
105
106 mmr_off = addr - rtc->base;
107 valuep = (void *)((unsigned long)rtc + mmr_base() + mmr_off);
108 value16p = valuep;
109 value32p = valuep;
110
111 HW_TRACE_READ ();
112
113 switch (mmr_off)
114 {
115 case mmr_offset(stat):
116 {
117 time_t t = time (NULL);
118 struct tm *tm = localtime (&t);
119 bu32 value =
120 (((tm->tm_year - 70) * 365 + tm->tm_yday) << 17) |
121 (tm->tm_hour << 12) |
122 (tm->tm_min << 6) |
123 (tm->tm_sec << 0);
124 dv_store_4 (dest, value);
125 break;
126 }
127 case mmr_offset(alarm):
128 dv_store_4 (dest, *value32p);
129 break;
130 case mmr_offset(istat):
131 case mmr_offset(ictl):
132 case mmr_offset(swcnt):
133 case mmr_offset(pren):
134 dv_store_2 (dest, *value16p);
135 break;
136 }
137
138 return nr_bytes;
139 }
140
141 static const struct hw_port_descriptor bfin_rtc_ports[] = {
142 { "rtc", 0, 0, output_port, },
143 { NULL, 0, 0, 0, },
144 };
145
146 static void
147 attach_bfin_rtc_regs (struct hw *me, struct bfin_rtc *rtc)
148 {
149 address_word attach_address;
150 int attach_space;
151 unsigned attach_size;
152 reg_property_spec reg;
153
154 if (hw_find_property (me, "reg") == NULL)
155 hw_abort (me, "Missing \"reg\" property");
156
157 if (!hw_find_reg_array_property (me, "reg", 0, &reg))
158 hw_abort (me, "\"reg\" property must contain three addr/size entries");
159
160 hw_unit_address_to_attach_address (hw_parent (me),
161 &reg.address,
162 &attach_space, &attach_address, me);
163 hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
164
165 if (attach_size != BFIN_MMR_RTC_SIZE)
166 hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_RTC_SIZE);
167
168 hw_attach_address (hw_parent (me),
169 0, attach_space, attach_address, attach_size, me);
170
171 rtc->base = attach_address;
172 }
173
174 static void
175 bfin_rtc_finish (struct hw *me)
176 {
177 struct bfin_rtc *rtc;
178
179 rtc = HW_ZALLOC (me, struct bfin_rtc);
180
181 set_hw_data (me, rtc);
182 set_hw_io_read_buffer (me, bfin_rtc_io_read_buffer);
183 set_hw_io_write_buffer (me, bfin_rtc_io_write_buffer);
184 set_hw_ports (me, bfin_rtc_ports);
185
186 attach_bfin_rtc_regs (me, rtc);
187
188 /* Initialize the RTC. */
189 }
190
191 const struct hw_descriptor dv_bfin_rtc_descriptor[] = {
192 {"bfin_rtc", bfin_rtc_finish,},
193 {NULL, NULL},
194 };
This page took 0.039791 seconds and 5 git commands to generate.