sim: bfin: add GPIO device simulation
[deliverable/binutils-gdb.git] / sim / bfin / dv-bfin_gpio.c
CommitLineData
b5215db0
MF
1/* Blackfin General Purpose Ports (GPIO) 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 "sim-main.h"
24#include "devices.h"
25#include "dv-bfin_gpio.h"
26
27struct bfin_gpio
28{
29 bu32 base;
30
31 /* Order after here is important -- matches hardware MMR layout. */
32 bu16 BFIN_MMR_16(data);
33 bu16 BFIN_MMR_16(clear);
34 bu16 BFIN_MMR_16(set);
35 bu16 BFIN_MMR_16(toggle);
36 bu16 BFIN_MMR_16(maska);
37 bu16 BFIN_MMR_16(maska_clear);
38 bu16 BFIN_MMR_16(maska_set);
39 bu16 BFIN_MMR_16(maska_toggle);
40 bu16 BFIN_MMR_16(maskb);
41 bu16 BFIN_MMR_16(maskb_clear);
42 bu16 BFIN_MMR_16(maskb_set);
43 bu16 BFIN_MMR_16(maskb_toggle);
44 bu16 BFIN_MMR_16(dir);
45 bu16 BFIN_MMR_16(polar);
46 bu16 BFIN_MMR_16(edge);
47 bu16 BFIN_MMR_16(both);
48 bu16 BFIN_MMR_16(inen);
49};
50#define mmr_base() offsetof(struct bfin_gpio, data)
51#define mmr_offset(mmr) (offsetof(struct bfin_gpio, mmr) - mmr_base())
52
53static const char * const mmr_names[] =
54{
55 "PORTIO", "PORTIO_CLEAR", "PORTIO_SET", "PORTIO_TOGGLE", "PORTIO_MASKA",
56 "PORTIO_MASKA_CLEAR", "PORTIO_MASKA_SET", "PORTIO_MASKA_TOGGLE",
57 "PORTIO_MASKB", "PORTIO_MASKB_CLEAR", "PORTIO_MASKB_SET",
58 "PORTIO_MASKB_TOGGLE", "PORTIO_DIR", "PORTIO_POLAR", "PORTIO_EDGE",
59 "PORTIO_BOTH", "PORTIO_INEN",
60};
61#define mmr_name(off) mmr_names[(off) / 4]
62
63static unsigned
64bfin_gpio_io_write_buffer (struct hw *me, const void *source, int space,
65 address_word addr, unsigned nr_bytes)
66{
67 struct bfin_gpio *port = hw_data (me);
68 bu32 mmr_off;
69 bu16 value;
70 bu16 *valuep;
71
72 value = dv_load_2 (source);
73 mmr_off = addr - port->base;
74 valuep = (void *)((unsigned long)port + mmr_base() + mmr_off);
75
76 HW_TRACE_WRITE ();
77
78 dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
79
80 switch (mmr_off)
81 {
82 case mmr_offset(data):
83 case mmr_offset(maska):
84 case mmr_offset(maskb):
85 case mmr_offset(dir):
86 case mmr_offset(polar):
87 case mmr_offset(edge):
88 case mmr_offset(both):
89 case mmr_offset(inen):
90 *valuep = value;
91 break;
92 case mmr_offset(clear):
93 case mmr_offset(maska_clear):
94 case mmr_offset(maskb_clear):
95 dv_w1c_2 (valuep, value, 0);
96 break;
97 case mmr_offset(set):
98 case mmr_offset(maska_set):
99 case mmr_offset(maskb_set):
100 *valuep |= value;
101 break;
102 case mmr_offset(toggle):
103 case mmr_offset(maska_toggle):
104 case mmr_offset(maskb_toggle):
105 *valuep ^= value;
106 break;
107 default:
108 dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
109 break;
110 }
111
112 return nr_bytes;
113}
114
115static unsigned
116bfin_gpio_io_read_buffer (struct hw *me, void *dest, int space,
117 address_word addr, unsigned nr_bytes)
118{
119 struct bfin_gpio *port = hw_data (me);
120 bu32 mmr_off;
121 bu16 *valuep;
122
123 mmr_off = addr - port->base;
124 valuep = (void *)((unsigned long)port + mmr_base() + mmr_off);
125
126 HW_TRACE_READ ();
127
128 dv_bfin_mmr_require_16 (me, addr, nr_bytes, false);
129
130 switch (mmr_off)
131 {
132 case mmr_offset(data):
133 case mmr_offset(clear):
134 case mmr_offset(set):
135 case mmr_offset(toggle):
136 dv_store_2 (dest, port->data);
137 break;
138 case mmr_offset(maska):
139 case mmr_offset(maska_clear):
140 case mmr_offset(maska_set):
141 case mmr_offset(maska_toggle):
142 dv_store_2 (dest, port->maska);
143 break;
144 case mmr_offset(maskb):
145 case mmr_offset(maskb_clear):
146 case mmr_offset(maskb_set):
147 case mmr_offset(maskb_toggle):
148 dv_store_2 (dest, port->maskb);
149 break;
150 case mmr_offset(dir):
151 case mmr_offset(polar):
152 case mmr_offset(edge):
153 case mmr_offset(both):
154 case mmr_offset(inen):
155 dv_store_2 (dest, *valuep);
156 break;
157 default:
158 dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
159 break;
160 }
161
162 return nr_bytes;
163}
164
165static const struct hw_port_descriptor bfin_gpio_ports[] =
166{
167 { "mask_a", 0, 0, output_port, },
168 { "mask_b", 1, 0, output_port, },
169 { "p0", 0, 0, input_port, },
170 { "p1", 1, 0, input_port, },
171 { "p2", 2, 0, input_port, },
172 { "p3", 3, 0, input_port, },
173 { "p4", 4, 0, input_port, },
174 { "p5", 5, 0, input_port, },
175 { "p6", 6, 0, input_port, },
176 { "p7", 7, 0, input_port, },
177 { "p8", 8, 0, input_port, },
178 { "p9", 9, 0, input_port, },
179 { "p10", 10, 0, input_port, },
180 { "p11", 11, 0, input_port, },
181 { "p12", 12, 0, input_port, },
182 { "p13", 13, 0, input_port, },
183 { "p14", 14, 0, input_port, },
184 { NULL, 0, 0, 0, },
185};
186
187static void
188bfin_gpio_port_event (struct hw *me, int my_port, struct hw *source,
189 int source_port, int level)
190{
191 struct bfin_gpio *port = hw_data (me);
192 bool olvl, nlvl;
193 bu32 bit = (1 << my_port);
194
195 /* Only screw with state if this pin is set as an input. */
196 if (!(port->dir & port->inen & bit))
197 return;
198
199 /* Get the old pin state for calculating an interrupt. */
200 olvl = !!(port->data & bit);
201
202 /* Update the new pin state. */
203 port->data = (port->data & ~bit) | (level << bit);
204
205 /* See if this state transition will generate an interrupt. */
206 nlvl = !!(port->data & bit);
207
208 if (port->edge & bit)
209 {
210 /* Pin is edge triggered. */
211 if (!(port->both & bit))
212 {
213 /* Both edges. */
214 if (olvl == nlvl)
215 return;
216 }
217 else
218 {
219 /* Just one edge. */
220 if (!(((port->polar & bit) && olvl > nlvl)
221 || (!(port->polar & bit) && olvl < nlvl)))
222 return;
223 }
224 }
225 else
226 {
227 /* Pin is level triggered. */
228 if (nlvl == !!(port->polar & bit))
229 return;
230 }
231
232 /* If the masks allow it, push the interrupt even higher. */
233 if (port->maska & bit)
234 hw_port_event (me, 0, 1);
235 if (port->maskb & bit)
236 hw_port_event (me, 1, 1);
237}
238
239static void
240attach_bfin_gpio_regs (struct hw *me, struct bfin_gpio *port)
241{
242 address_word attach_address;
243 int attach_space;
244 unsigned attach_size;
245 reg_property_spec reg;
246
247 if (hw_find_property (me, "reg") == NULL)
248 hw_abort (me, "Missing \"reg\" property");
249
250 if (!hw_find_reg_array_property (me, "reg", 0, &reg))
251 hw_abort (me, "\"reg\" property must contain three addr/size entries");
252
253 hw_unit_address_to_attach_address (hw_parent (me),
254 &reg.address,
255 &attach_space, &attach_address, me);
256 hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
257
258 if (attach_size != BFIN_MMR_GPIO_SIZE)
259 hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_GPIO_SIZE);
260
261 hw_attach_address (hw_parent (me),
262 0, attach_space, attach_address, attach_size, me);
263
264 port->base = attach_address;
265}
266
267static void
268bfin_gpio_finish (struct hw *me)
269{
270 struct bfin_gpio *port;
271
272 port = HW_ZALLOC (me, struct bfin_gpio);
273
274 set_hw_data (me, port);
275 set_hw_io_read_buffer (me, bfin_gpio_io_read_buffer);
276 set_hw_io_write_buffer (me, bfin_gpio_io_write_buffer);
277 set_hw_ports (me, bfin_gpio_ports);
278 set_hw_port_event (me, bfin_gpio_port_event);
279
280 attach_bfin_gpio_regs (me, port);
281}
282
283const struct hw_descriptor dv_bfin_gpio_descriptor[] =
284{
285 {"bfin_gpio", bfin_gpio_finish,},
286 {NULL, NULL},
287};
This page took 0.033793 seconds and 4 git commands to generate.