1 /* Lattice Mico32 timer model.
2 Contributed by Jon Beniston <jon@beniston.com>
4 Copyright (C) 2009-2023 Free Software Foundation, Inc.
6 This file is part of GDB.
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 "sim-assert.h"
30 unsigned base
; /* Base address of this timer. */
31 unsigned limit
; /* Limit address of this timer. */
35 unsigned int snapshot
;
36 struct hw_event
*event
;
39 /* Timer registers. */
40 #define LM32_TIMER_STATUS 0x0
41 #define LM32_TIMER_CONTROL 0x4
42 #define LM32_TIMER_PERIOD 0x8
43 #define LM32_TIMER_SNAPSHOT 0xc
52 static const struct hw_port_descriptor lm32timer_ports
[] = {
53 {"int", INT_PORT
, 0, output_port
},
58 do_timer_event (struct hw
*me
, void *data
)
60 struct lm32timer
*timer
= hw_data (me
);
62 /* Is timer started? */
63 if (timer
->control
& 0x4)
67 /* Decrement timer. */
70 else if (timer
->control
& 1)
73 timer
->snapshot
= timer
->period
;
76 /* Generate interrupt when timer is at 0, and interrupt enable is 1. */
77 if ((timer
->snapshot
== 0) && (timer
->control
& 1))
79 /* Generate interrupt. */
80 hw_port_event (me
, INT_PORT
, 1);
82 /* If timer is started, schedule another event to decrement the timer again. */
83 if (timer
->control
& 4)
84 hw_event_queue_schedule (me
, 1, do_timer_event
, 0);
88 lm32timer_io_write_buffer (struct hw
*me
,
90 int space
, unsigned_word base
, unsigned nr_bytes
)
92 struct lm32timer
*timers
= hw_data (me
);
94 const unsigned char *source_bytes
= source
;
97 HW_TRACE ((me
, "write to 0x%08lx length %d with 0x%x", (long) base
,
98 (int) nr_bytes
, value
));
101 value
= (source_bytes
[0] << 24)
102 | (source_bytes
[1] << 16) | (source_bytes
[2] << 8) | (source_bytes
[3]);
104 hw_abort (me
, "write with invalid number of bytes: %d", nr_bytes
);
106 timer_reg
= base
- timers
->base
;
110 case LM32_TIMER_STATUS
:
111 timers
->status
= value
;
113 case LM32_TIMER_CONTROL
:
114 timers
->control
= value
;
115 if (timers
->control
& 0x4)
117 /* Timer is started. */
118 hw_event_queue_schedule (me
, 1, do_timer_event
, 0);
121 case LM32_TIMER_PERIOD
:
122 timers
->period
= value
;
125 hw_abort (me
, "invalid register address: 0x%x.", timer_reg
);
132 lm32timer_io_read_buffer (struct hw
*me
,
134 int space
, unsigned_word base
, unsigned nr_bytes
)
136 struct lm32timer
*timers
= hw_data (me
);
139 unsigned char *dest_bytes
= dest
;
141 HW_TRACE ((me
, "read 0x%08lx length %d", (long) base
, (int) nr_bytes
));
143 timer_reg
= base
- timers
->base
;
147 case LM32_TIMER_STATUS
:
148 value
= timers
->status
;
150 case LM32_TIMER_CONTROL
:
151 value
= timers
->control
;
153 case LM32_TIMER_PERIOD
:
154 value
= timers
->period
;
156 case LM32_TIMER_SNAPSHOT
:
157 value
= timers
->snapshot
;
160 hw_abort (me
, "invalid register address: 0x%x.", timer_reg
);
165 dest_bytes
[0] = value
>> 24;
166 dest_bytes
[1] = value
>> 16;
167 dest_bytes
[2] = value
>> 8;
168 dest_bytes
[3] = value
;
171 hw_abort (me
, "read of unsupported number of bytes: %d", nr_bytes
);
177 attach_lm32timer_regs (struct hw
*me
, struct lm32timer
*timers
)
179 unsigned_word attach_address
;
181 unsigned attach_size
;
182 reg_property_spec reg
;
184 if (hw_find_property (me
, "reg") == NULL
)
185 hw_abort (me
, "Missing \"reg\" property");
186 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
187 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
188 hw_unit_address_to_attach_address (hw_parent (me
),
190 &attach_space
, &attach_address
, me
);
191 timers
->base
= attach_address
;
192 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
193 timers
->limit
= attach_address
+ (attach_size
- 1);
194 hw_attach_address (hw_parent (me
),
195 0, attach_space
, attach_address
, attach_size
, me
);
199 lm32timer_finish (struct hw
*me
)
201 struct lm32timer
*timers
;
204 timers
= HW_ZALLOC (me
, struct lm32timer
);
205 set_hw_data (me
, timers
);
206 set_hw_io_read_buffer (me
, lm32timer_io_read_buffer
);
207 set_hw_io_write_buffer (me
, lm32timer_io_write_buffer
);
208 set_hw_ports (me
, lm32timer_ports
);
210 /* Attach ourself to our parent bus. */
211 attach_lm32timer_regs (me
, timers
);
213 /* Initialize the timers. */
217 timers
->snapshot
= 0;
220 const struct hw_descriptor dv_lm32timer_descriptor
[] = {
221 {"lm32timer", lm32timer_finish
,},