1 /* This file is part of the program GDB, the GNU debugger.
3 Copyright (C) 1998-2024 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* This must come before any other includes. */
31 tx3904tmr - tx3904 timer
37 Implements one tx3904 timer/counter described in the tx3904
38 user guide. Three instances are required for TMR0, TMR1, and
39 TMR3 within the tx3904, at different base addresses.
41 Both internal and system clocks are synthesized as divided versions
42 of the simulator clock.
44 There is no support for:
45 - edge sensitivity of external clock
46 - different mode restrictions for TMR0..2
47 - level interrupts (interrupts are treated as events that occur at edges)
56 Base of TMR control register bank. <length> must equal 0x100.
57 Register offsets: 0: TCR: timer control register
58 4: TISR: timer interrupt status register
59 8: CPRA: compare register A
60 12: CPRB: compare register B
61 16: ITMR: interval timer mode register
62 32: CCDR: divider register
63 48: PMGR: pulse generator mode register
64 64: WTMR: watchdog timer mode register
65 240: TRR: timer read register
70 Rate of timer clock signal. This number is the number of simulator
71 ticks per clock signal tick. Default 1.
76 Rate of "external input clock signal", the other clock input of the
77 timer. It uses the same scale as above. Default 100.
86 Interrupt port. An event is generated when a timer interrupt
92 Flip-flop output, corresponds to the TMFFOUT port. An event is
93 generated when flip-flop changes value. The integer associated
94 with the event is 1/0 according to flip-flop value.
105 /* static functions */
107 static void deliver_tx3904tmr_tick (struct hw
*me
, void *data
);
110 /* register numbers; each is one word long */
136 static const struct hw_port_descriptor tx3904tmr_ports
[] =
138 { "int", INT_PORT
, 0, output_port
, },
139 { "ff", FF_PORT
, 0, output_port
, },
140 { "reset", RESET_PORT
, 0, input_port
, },
146 /* The timer/counter register internal state. Note that we store
147 state using the control register images, in host endian order. */
150 address_word base_address
; /* control register base */
151 unsigned_4 clock_ticks
, ext_ticks
; /* clock frequencies */
152 signed_8 last_ticks
; /* time at last deliver_*_tick call */
153 signed_8 roundoff_ticks
; /* sim ticks unprocessed during last tick call */
154 int ff
; /* pulse generator flip-flop value: 1/0 */
155 struct hw_event
* event
; /* last scheduled event */
158 #define GET_TCR_TCE(c) (((c)->tcr & 0x80) >> 7)
159 #define GET_TCR_CCDE(c) (((c)->tcr & 0x40) >> 6)
160 #define GET_TCR_CRE(c) (((c)->tcr & 0x20) >> 5)
161 #define GET_TCR_CCS(c) (((c)->tcr & 0x04) >> 2)
162 #define GET_TCR_TMODE(c) (((c)->tcr & 0x03) >> 0)
164 #define SET_TISR_TWIS(c) ((c)->tisr |= 0x08)
165 #define SET_TISR_TPIBS(c) ((c)->tisr |= 0x04)
166 #define SET_TISR_TPIAS(c) ((c)->tisr |= 0x02)
167 #define SET_TISR_TIIS(c) ((c)->tisr |= 0x01)
171 #define GET_ITMR_TIIE(c) (((c)->itmr & 0x8000) >> 15)
172 #define SET_ITMR_TIIE(c,v) BLIT32((c)->itmr, 15, (v) ? 1 : 0)
173 #define GET_ITMR_TZCE(c) (((c)->itmr & 0x0001) >> 0)
174 #define SET_ITMR_TZCE(c,v) BLIT32((c)->itmr, 0, (v) ? 1 : 0)
176 #define GET_CCDR_CDR(c) (((c)->ccdr & 0x07) >> 0)
178 #define GET_PMGR_TPIBE(c) (((c)->pmgr & 0x8000) >> 15)
179 #define SET_PMGR_TPIBE(c,v) BLIT32((c)->pmgr, 15, (v) ? 1 : 0)
180 #define GET_PMGR_TPIAE(c) (((c)->pmgr & 0x4000) >> 14)
181 #define SET_PMGR_TPIAE(c,v) BLIT32((c)->pmgr, 14, (v) ? 1 : 0)
182 #define GET_PMGR_FFI(c) (((c)->pmgr & 0x0001) >> 0)
183 #define SET_PMGR_FFI(c,v) BLIT32((c)->pmgr, 0, (v) ? 1 : 0)
185 #define GET_WTMR_TWIE(c) (((c)->wtmr & 0x8000) >> 15)
186 #define SET_WTMR_TWIE(c,v) BLIT32((c)->wtmr, 15, (v) ? 1 : 0)
187 #define GET_WTMR_WDIS(c) (((c)->wtmr & 0x0080) >> 7)
188 #define SET_WTMR_WDIS(c,v) BLIT32((c)->wtmr, 7, (v) ? 1 : 0)
189 #define GET_WTMR_TWC(c) (((c)->wtmr & 0x0001) >> 0)
190 #define SET_WTMR_TWC(c,v) BLIT32((c)->wtmr, 0, (v) ? 1 : 0)
196 /* Finish off the partially created hw device. Attach our local
197 callbacks. Wire up our port names etc */
199 static hw_io_read_buffer_method tx3904tmr_io_read_buffer
;
200 static hw_io_write_buffer_method tx3904tmr_io_write_buffer
;
201 static hw_port_event_method tx3904tmr_port_event
;
204 attach_tx3904tmr_regs (struct hw
*me
,
205 struct tx3904tmr
*controller
)
207 unsigned_word attach_address
;
209 unsigned attach_size
;
210 reg_property_spec reg
;
212 if (hw_find_property (me
, "reg") == NULL
)
213 hw_abort (me
, "Missing \"reg\" property");
215 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
216 hw_abort (me
, "\"reg\" property must contain one addr/size entry");
218 hw_unit_address_to_attach_address (hw_parent (me
),
223 hw_unit_size_to_attach_size (hw_parent (me
),
227 hw_attach_address (hw_parent (me
), 0,
228 attach_space
, attach_address
, attach_size
,
231 if (hw_find_property(me
, "clock") != NULL
)
232 controller
->clock_ticks
= (unsigned_4
) hw_find_integer_property(me
, "clock");
234 if (hw_find_property(me
, "ext") != NULL
)
235 controller
->ext_ticks
= (unsigned_4
) hw_find_integer_property(me
, "ext");
237 controller
->base_address
= attach_address
;
242 tx3904tmr_finish (struct hw
*me
)
244 struct tx3904tmr
*controller
;
246 controller
= HW_ZALLOC (me
, struct tx3904tmr
);
247 set_hw_data (me
, controller
);
248 set_hw_io_read_buffer (me
, tx3904tmr_io_read_buffer
);
249 set_hw_io_write_buffer (me
, tx3904tmr_io_write_buffer
);
250 set_hw_ports (me
, tx3904tmr_ports
);
251 set_hw_port_event (me
, tx3904tmr_port_event
);
253 /* Preset clock dividers */
254 controller
->clock_ticks
= 1;
255 controller
->ext_ticks
= 100;
257 /* Attach ourself to our parent bus */
258 attach_tx3904tmr_regs (me
, controller
);
260 /* Initialize to reset state */
268 controller
->cpra
= controller
->cprb
= 0x00FFFFFF;
270 controller
->last_ticks
= controller
->roundoff_ticks
= 0;
271 controller
->event
= NULL
;
276 /* An event arrives on an interrupt port */
279 tx3904tmr_port_event (struct hw
*me
,
285 struct tx3904tmr
*controller
= hw_data (me
);
291 HW_TRACE ((me
, "reset"));
293 /* preset flip-flop to FFI value */
294 controller
->ff
= GET_PMGR_FFI(controller
);
303 controller
->cpra
= controller
->cprb
= 0x00FFFFFF;
304 controller
->last_ticks
= controller
->roundoff_ticks
= 0;
305 if (controller
->event
!= NULL
)
306 hw_event_queue_deschedule(me
, controller
->event
);
307 controller
->event
= NULL
;
312 hw_abort (me
, "Event on unknown port %d", my_port
);
318 /* generic read/write */
321 tx3904tmr_io_read_buffer (struct hw
*me
,
327 struct tx3904tmr
*controller
= hw_data (me
);
330 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
331 for (byte
= 0; byte
< nr_bytes
; byte
++)
333 address_word address
= base
+ byte
;
334 int reg_number
= (address
- controller
->base_address
) / 4;
335 int reg_offset
= 3 - (address
- controller
->base_address
) % 4;
336 unsigned_4 register_value
; /* in target byte order */
338 /* fill in entire register_value word */
341 case TCR_REG
: register_value
= controller
->tcr
; break;
342 case TISR_REG
: register_value
= controller
->tisr
; break;
343 case CPRA_REG
: register_value
= controller
->cpra
; break;
344 case CPRB_REG
: register_value
= controller
->cprb
; break;
345 case ITMR_REG
: register_value
= controller
->itmr
; break;
346 case CCDR_REG
: register_value
= controller
->ccdr
; break;
347 case PMGR_REG
: register_value
= controller
->pmgr
; break;
348 case WTMR_REG
: register_value
= controller
->wtmr
; break;
349 case TRR_REG
: register_value
= controller
->trr
; break;
350 default: register_value
= 0;
353 /* write requested byte out */
354 memcpy ((char*) dest
+ byte
, ((char*)& register_value
)+reg_offset
, 1);
363 tx3904tmr_io_write_buffer (struct hw
*me
,
369 struct tx3904tmr
*controller
= hw_data (me
);
372 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
373 for (byte
= 0; byte
< nr_bytes
; byte
++)
375 address_word address
= base
+ byte
;
376 unsigned_1 write_byte
= ((const char*) source
)[byte
];
377 int reg_number
= (address
- controller
->base_address
) / 4;
378 int reg_offset
= 3 - (address
- controller
->base_address
) % 4;
380 /* fill in entire register_value word */
384 if (reg_offset
== 0) /* first byte */
386 /* update register, but mask out NOP bits */
387 controller
->tcr
= (unsigned_4
) (write_byte
& 0xef);
389 /* Reset counter value if timer suspended and CRE is set. */
390 if (GET_TCR_TCE(controller
) == 0 &&
391 GET_TCR_CRE(controller
) == 1)
394 /* HW_TRACE ((me, "tcr: %08lx", (long) controller->tcr)); */
398 if (reg_offset
== 1) /* second byte */
400 SET_ITMR_TIIE(controller
, write_byte
& 0x80);
402 else if (reg_offset
== 0) /* first byte */
404 SET_ITMR_TZCE(controller
, write_byte
& 0x01);
406 /* HW_TRACE ((me, "itmr: %08lx", (long) controller->itmr)); */
410 if (reg_offset
== 0) /* first byte */
412 controller
->ccdr
= write_byte
& 0x07;
414 /* HW_TRACE ((me, "ccdr: %08lx", (long) controller->ccdr)); */
418 if (reg_offset
== 1) /* second byte */
420 SET_PMGR_TPIBE(controller
, write_byte
& 0x80);
421 SET_PMGR_TPIAE(controller
, write_byte
& 0x40);
423 else if (reg_offset
== 0) /* first byte */
425 SET_PMGR_FFI(controller
, write_byte
& 0x01);
427 /* HW_TRACE ((me, "pmgr: %08lx", (long) controller->pmgr)); */
431 if (reg_offset
== 1) /* second byte */
433 SET_WTMR_TWIE(controller
, write_byte
& 0x80);
435 else if (reg_offset
== 0) /* first byte */
437 SET_WTMR_WDIS(controller
, write_byte
& 0x80);
438 SET_WTMR_TWC(controller
, write_byte
& 0x01);
440 /* HW_TRACE ((me, "wtmr: %08lx", (long) controller->wtmr)); */
444 if (reg_offset
== 0) /* first byte */
446 /* All bits must be zero in given byte, according to
449 /* Send an "interrupt off" event on the interrupt port */
450 if (controller
->tisr
!= 0) /* any interrupts active? */
452 hw_port_event (me
, INT_PORT
, 0);
455 /* clear interrupt status register */
456 controller
->tisr
= 0;
458 /* HW_TRACE ((me, "tisr: %08lx", (long) controller->tisr)); */
462 if (reg_offset
< 3) /* first, second, or third byte */
464 MBLIT32(controller
->cpra
, (reg_offset
*8)+7, (reg_offset
*8), write_byte
);
466 /* HW_TRACE ((me, "cpra: %08lx", (long) controller->cpra)); */
470 if (reg_offset
< 3) /* first, second, or third byte */
472 MBLIT32(controller
->cprb
, (reg_offset
*8)+7, (reg_offset
*8), write_byte
);
474 /* HW_TRACE ((me, "cprb: %08lx", (long) controller->cprb)); */
478 HW_TRACE ((me
, "write to illegal register %d", reg_number
));
480 } /* loop over bytes */
482 /* Schedule a timer event in near future, so we can increment or
483 stop the counter, to respond to register updates. */
484 hw_event_queue_schedule(me
, 1, deliver_tx3904tmr_tick
, NULL
);
491 /* Deliver a clock tick to the counter. */
493 deliver_tx3904tmr_tick (struct hw
*me
,
496 struct tx3904tmr
*controller
= hw_data (me
);
497 SIM_DESC sd
= hw_system (me
);
498 signed_8 this_ticks
= sim_events_time(sd
);
502 signed_8 quotient
, remainder
;
504 /* compute simulation ticks between last tick and this tick */
505 if (controller
->last_ticks
!= 0)
506 warp
= this_ticks
- controller
->last_ticks
+ controller
->roundoff_ticks
;
509 controller
->last_ticks
= this_ticks
; /* initialize */
510 warp
= controller
->roundoff_ticks
;
513 if (controller
->event
!= NULL
)
514 hw_event_queue_deschedule(me
, controller
->event
);
515 controller
->event
= NULL
;
517 /* Check whether the timer ticking is enabled at this moment. This
518 largely a function of the TCE bit, but is also slightly
520 switch ((int) GET_TCR_TMODE(controller
))
522 case 0: /* interval */
523 /* do not advance counter if TCE = 0 or if holding at count = CPRA */
524 if (GET_TCR_TCE(controller
) == 0 ||
525 controller
->trr
== controller
->cpra
)
529 case 1: /* pulse generator */
530 /* do not advance counter if TCE = 0 */
531 if (GET_TCR_TCE(controller
) == 0)
535 case 2: /* watchdog */
536 /* do not advance counter if TCE = 0 and WDIS = 1 */
537 if (GET_TCR_TCE(controller
) == 0 &&
538 GET_WTMR_WDIS(controller
) == 1)
542 case 3: /* disabled */
543 /* regardless of TCE, do not advance counter */
547 /* In any of the above cases that return, a subsequent register
548 write will be needed to restart the timer. A tick event is
549 scheduled by any register write, so it is more efficient not to
550 reschedule dummy events here. */
553 /* find appropriate divisor etc. */
554 if (GET_TCR_CCS(controller
) == 0) /* internal system clock */
556 /* apply internal clock divider */
557 if (GET_TCR_CCDE(controller
)) /* divisor circuit enabled? */
558 divisor
= controller
->clock_ticks
* (1 << (1 + GET_CCDR_CDR(controller
)));
560 divisor
= controller
->clock_ticks
;
564 divisor
= controller
->ext_ticks
;
567 /* how many times to increase counter? */
568 quotient
= warp
/ divisor
;
569 remainder
= warp
% divisor
;
571 /* NOTE: If the event rescheduling code works properly, the quotient
572 should never be larger than 1. That is, we should receive events
573 here at least as frequently as the simulated counter is supposed
574 to decrement. So the remainder (-> roundoff_ticks) will slowly
575 accumulate, with the quotient == 0. Once in a while, quotient
578 controller
->roundoff_ticks
= remainder
;
579 controller
->last_ticks
= this_ticks
;
580 while(quotient
> 0) /* Is it time to increment counter? */
582 /* next 24-bit counter value */
583 unsigned_4 next_trr
= (controller
->trr
+ 1) % (1 << 24);
586 switch ((int) GET_TCR_TMODE(controller
))
588 case 0: /* interval timer mode */
590 /* Current or next counter value matches CPRA value? The
591 first case covers counter holding at maximum before
592 reset. The second case covers normal counting
594 if (controller
->trr
== controller
->cpra
||
595 next_trr
== controller
->cpra
)
597 /* likely hold CPRA value */
598 if (controller
->trr
== controller
->cpra
)
599 next_trr
= controller
->cpra
;
601 SET_TISR_TIIS(controller
);
603 /* Signal an interrupt if it is enabled with TIIE,
604 and if we just arrived at CPRA. Don't repeatedly
605 interrupt if holding due to TZCE=0 */
606 if (GET_ITMR_TIIE(controller
) &&
607 next_trr
!= controller
->trr
)
609 hw_port_event(me
, INT_PORT
, 1);
613 if (GET_ITMR_TZCE(controller
))
621 case 1: /* pulse generator mode */
623 /* first trip point */
624 if (next_trr
== controller
->cpra
)
626 /* flip flip-flop & report */
628 hw_port_event(me
, FF_PORT
, controller
->ff
);
629 SET_TISR_TPIAS(controller
);
631 /* signal interrupt */
632 if (GET_PMGR_TPIAE(controller
))
634 hw_port_event(me
, INT_PORT
, 1);
638 /* second trip point */
639 else if (next_trr
== controller
->cprb
)
641 /* flip flip-flop & report */
643 hw_port_event(me
, FF_PORT
, controller
->ff
);
644 SET_TISR_TPIBS(controller
);
646 /* signal interrupt */
647 if (GET_PMGR_TPIBE(controller
))
649 hw_port_event(me
, INT_PORT
, 1);
658 case 2: /* watchdog timer mode */
660 /* watchdog timer expiry */
661 if (next_trr
== controller
->cpra
)
663 SET_TISR_TWIS(controller
);
665 /* signal interrupt */
666 if (GET_WTMR_TWIE(controller
))
668 hw_port_event(me
, INT_PORT
, 1);
677 case 3: /* disabled */
682 /* update counter and report */
683 controller
->trr
= next_trr
;
684 /* HW_TRACE ((me, "counter trr %ld tisr %lx",
685 (long) controller->trr, (long) controller->tisr)); */
686 } /* end quotient loop */
688 /* Reschedule a timer event in near future, so we can increment the
689 counter again. Set the event about 75% of divisor time away, so
690 we will experience roughly 1.3 events per counter increment. */
691 controller
->event
= hw_event_queue_schedule(me
, divisor
*3/4, deliver_tx3904tmr_tick
, NULL
);
697 const struct hw_descriptor dv_tx3904tmr_descriptor
[] = {
698 { "tx3904tmr", tx3904tmr_finish
, },