1 /* Lattice Mico32 timer model.
2 Contributed by Jon Beniston <jon@beniston.com>
4 Copyright (C) 2009-2020 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/>. */
23 #include "sim-assert.h"
27 unsigned base
; /* Base address of this timer. */
28 unsigned limit
; /* Limit address of this timer. */
32 unsigned int snapshot
;
33 struct hw_event
*event
;
36 /* Timer registers. */
37 #define LM32_TIMER_STATUS 0x0
38 #define LM32_TIMER_CONTROL 0x4
39 #define LM32_TIMER_PERIOD 0x8
40 #define LM32_TIMER_SNAPSHOT 0xc
49 static const struct hw_port_descriptor lm32timer_ports
[] = {
50 {"int", INT_PORT
, 0, output_port
},
55 do_timer_event (struct hw
*me
, void *data
)
57 struct lm32timer
*timer
= hw_data (me
);
59 /* Is timer started? */
60 if (timer
->control
& 0x4)
64 /* Decrement timer. */
67 else if (timer
->control
& 1)
70 timer
->snapshot
= timer
->period
;
73 /* Generate interrupt when timer is at 0, and interrupt enable is 1. */
74 if ((timer
->snapshot
== 0) && (timer
->control
& 1))
76 /* Generate interrupt. */
77 hw_port_event (me
, INT_PORT
, 1);
79 /* If timer is started, schedule another event to decrement the timer again. */
80 if (timer
->control
& 4)
81 hw_event_queue_schedule (me
, 1, do_timer_event
, 0);
85 lm32timer_io_write_buffer (struct hw
*me
,
87 int space
, unsigned_word base
, unsigned nr_bytes
)
89 struct lm32timer
*timers
= hw_data (me
);
91 const unsigned char *source_bytes
= source
;
94 HW_TRACE ((me
, "write to 0x%08lx length %d with 0x%x", (long) base
,
95 (int) nr_bytes
, value
));
98 value
= (source_bytes
[0] << 24)
99 | (source_bytes
[1] << 16) | (source_bytes
[2] << 8) | (source_bytes
[3]);
101 hw_abort (me
, "write with invalid number of bytes: %d", nr_bytes
);
103 timer_reg
= base
- timers
->base
;
107 case LM32_TIMER_STATUS
:
108 timers
->status
= value
;
110 case LM32_TIMER_CONTROL
:
111 timers
->control
= value
;
112 if (timers
->control
& 0x4)
114 /* Timer is started. */
115 hw_event_queue_schedule (me
, 1, do_timer_event
, 0);
118 case LM32_TIMER_PERIOD
:
119 timers
->period
= value
;
122 hw_abort (me
, "invalid register address: 0x%x.", timer_reg
);
129 lm32timer_io_read_buffer (struct hw
*me
,
131 int space
, unsigned_word base
, unsigned nr_bytes
)
133 struct lm32timer
*timers
= hw_data (me
);
136 unsigned char *dest_bytes
= dest
;
138 HW_TRACE ((me
, "read 0x%08lx length %d", (long) base
, (int) nr_bytes
));
140 timer_reg
= base
- timers
->base
;
144 case LM32_TIMER_STATUS
:
145 value
= timers
->status
;
147 case LM32_TIMER_CONTROL
:
148 value
= timers
->control
;
150 case LM32_TIMER_PERIOD
:
151 value
= timers
->period
;
153 case LM32_TIMER_SNAPSHOT
:
154 value
= timers
->snapshot
;
157 hw_abort (me
, "invalid register address: 0x%x.", timer_reg
);
162 dest_bytes
[0] = value
>> 24;
163 dest_bytes
[1] = value
>> 16;
164 dest_bytes
[2] = value
>> 8;
165 dest_bytes
[3] = value
;
168 hw_abort (me
, "read of unsupported number of bytes: %d", nr_bytes
);
174 attach_lm32timer_regs (struct hw
*me
, struct lm32timer
*timers
)
176 unsigned_word attach_address
;
178 unsigned attach_size
;
179 reg_property_spec reg
;
181 if (hw_find_property (me
, "reg") == NULL
)
182 hw_abort (me
, "Missing \"reg\" property");
183 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
184 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
185 hw_unit_address_to_attach_address (hw_parent (me
),
187 &attach_space
, &attach_address
, me
);
188 timers
->base
= attach_address
;
189 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
190 timers
->limit
= attach_address
+ (attach_size
- 1);
191 hw_attach_address (hw_parent (me
),
192 0, attach_space
, attach_address
, attach_size
, me
);
196 lm32timer_finish (struct hw
*me
)
198 struct lm32timer
*timers
;
201 timers
= HW_ZALLOC (me
, struct lm32timer
);
202 set_hw_data (me
, timers
);
203 set_hw_io_read_buffer (me
, lm32timer_io_read_buffer
);
204 set_hw_io_write_buffer (me
, lm32timer_io_write_buffer
);
205 set_hw_ports (me
, lm32timer_ports
);
207 /* Attach ourself to our parent bus. */
208 attach_lm32timer_regs (me
, timers
);
210 /* Initialize the timers. */
214 timers
->snapshot
= 0;
217 const struct hw_descriptor dv_lm32timer_descriptor
[] = {
218 {"lm32timer", lm32timer_finish
,},