1 /* This file is part of the program GDB, the GNU debugger.
3 Copyright (C) 1998 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 2 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 mn103tim - mn103002 timers (8 and 16 bit)
33 Implements the mn103002 8 and 16 bit timers as described in the mn103002 user guide.
38 reg = <8bit-timers-addr> <8bit-timers-size> <16bit-timers-addr> <16bit-timers-size>
46 /* The timers' register address blocks */
48 struct mn103tim_block
{
53 enum { TIMER8_BLOCK
, TIMER16_BLOCK
, NR_TIMER_BLOCKS
};
55 enum timer_register_types
{
57 TM0MD
= FIRST_MODE_REG
,
64 LAST_MODE_REG
= TM6MD
,
66 TM0BR
= FIRST_BASE_REG
,
72 LAST_BASE_REG
= TM5BR
,
74 TM0BC
= FIRST_COUNTER
,
89 /* Don't include timer 6 because it's handled specially. */
90 #define NR_8BIT_TIMERS 4
91 #define NR_16BIT_TIMERS 2
94 typedef struct _mn10300_timer
{
95 unsigned32 div_ratio
, start
, base
;
97 struct hw_event
*event
;
102 struct mn103tim_block block
[NR_TIMER_BLOCKS
];
103 mn10300_timer timer
[NR_TIMERS
];
105 /* treat timer 6 registers specially. */
106 unsigned16 tm6md
, tm6bc
, tm6mca
, tm6mcb
;
107 unsigned8 tm6mda
, tm6mdb
; /* compare/capture mode regs for timer 6 */
108 struct hw_event
*event6
;
111 /* output port ID's */
127 static const struct hw_port_descriptor mn103tim_ports
[] = {
129 { "timer-0-underflow", TIMER0_UFLOW
, 0, output_port
, },
130 { "timer-1-underflow", TIMER1_UFLOW
, 0, output_port
, },
131 { "timer-2-underflow", TIMER2_UFLOW
, 0, output_port
, },
132 { "timer-3-underflow", TIMER3_UFLOW
, 0, output_port
, },
133 { "timer-4-underflow", TIMER4_UFLOW
, 0, output_port
, },
134 { "timer-5-underflow", TIMER5_UFLOW
, 0, output_port
, },
136 { "timer-6-underflow", TIMER6_UFLOW
, 0, output_port
, },
137 { "timer-6-compare-a", TIMER6_CMPA
, 0, output_port
, },
138 { "timer-6-compare-b", TIMER6_CMPB
, 0, output_port
, },
143 #define bits2to5_mask 0x3c
144 #define load_mask 0x40
145 #define count_mask 0x80
146 #define count_and_load_mask (load_mask | count_mask)
147 #define clock_mask 0x03
148 #define clk_ioclk 0x00
149 #define clk_cascaded 0x03
152 /* Finish off the partially created hw device. Attach our local
153 callbacks. Wire up our port names etc */
155 static hw_io_read_buffer_method mn103tim_io_read_buffer
;
156 static hw_io_write_buffer_method mn103tim_io_write_buffer
;
159 attach_mn103tim_regs (struct hw
*me
,
160 struct mn103tim
*timers
)
163 if (hw_find_property (me
, "reg") == NULL
)
164 hw_abort (me
, "Missing \"reg\" property");
165 for (i
= 0; i
< NR_TIMER_BLOCKS
; i
++)
167 unsigned_word attach_address
;
169 unsigned attach_size
;
170 reg_property_spec reg
;
171 if (!hw_find_reg_array_property (me
, "reg", i
, ®
))
172 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
173 hw_unit_address_to_attach_address (hw_parent (me
),
178 timers
->block
[i
].base
= attach_address
;
179 hw_unit_size_to_attach_size (hw_parent (me
),
182 timers
->block
[i
].bound
= attach_address
+ (attach_size
- 1);
183 hw_attach_address (hw_parent (me
),
185 attach_space
, attach_address
, attach_size
,
191 mn103tim_finish (struct hw
*me
)
193 struct mn103tim
*timers
;
196 timers
= HW_ZALLOC (me
, struct mn103tim
);
197 set_hw_data (me
, timers
);
198 set_hw_io_read_buffer (me
, mn103tim_io_read_buffer
);
199 set_hw_io_write_buffer (me
, mn103tim_io_write_buffer
);
200 set_hw_ports (me
, mn103tim_ports
);
202 /* Attach ourself to our parent bus */
203 attach_mn103tim_regs (me
, timers
);
205 /* Initialize the timers */
206 for ( i
=0; i
< NR_TIMERS
; ++i
)
208 timers
->timer
[i
].event
= NULL
;
209 timers
->timer
[i
].mode
= 0x00;
210 timers
->timer
[i
].base
= 0;
211 timers
->timer
[i
].div_ratio
= 0;
212 timers
->timer
[i
].start
= 0;
214 timers
->tm6md
= 0x0000;
215 timers
->tm6bc
= 0x0000;
216 timers
->tm6mca
= 0x0000;
217 timers
->tm6mcb
= 0x0000;
218 timers
->tm6mda
= 0x00;
219 timers
->tm6mdb
= 0x00;
227 decode_addr (struct hw
*me
,
228 struct mn103tim
*timers
,
229 unsigned_word address
)
231 unsigned_word offset
;
232 offset
= address
- timers
->block
[0].base
;
236 case 0x00: return TM0MD
;
237 case 0x01: return TM1MD
;
238 case 0x02: return TM2MD
;
239 case 0x03: return TM3MD
;
240 case 0x10: return TM0BR
;
241 case 0x11: return TM1BR
;
242 case 0x12: return TM2BR
;
243 case 0x13: return TM3BR
;
244 case 0x20: return TM0BC
;
245 case 0x21: return TM1BC
;
246 case 0x22: return TM2BC
;
247 case 0x23: return TM3BC
;
248 case 0x80: return TM4MD
;
249 case 0x82: return TM5MD
;
250 case 0x84: return TM6MD
;
251 case 0x90: return TM4BR
;
252 case 0x92: return TM5BR
;
253 case 0xa0: return TM4BC
;
254 case 0xa2: return TM5BC
;
255 case 0xa4: return TM6BC
;
256 case 0xb4: return TM6MDA
;
257 case 0xb5: return TM6MDB
;
258 case 0xc4: return TM6CA
;
259 case 0xd4: return TM6CB
;
262 hw_abort (me
, "bad address");
269 read_mode_reg (struct hw
*me
,
270 struct mn103tim
*timers
,
281 /* Accessing 1 byte is ok for all mode registers. */
282 *(unsigned8
*)dest
= timers
->timer
[timer_nr
].mode
;
288 *(unsigned16
*)dest
= timers
->tm6md
;
290 else if ( timer_nr
== 0 || timer_nr
== 2 )
292 val16
= (timers
->timer
[timer_nr
].mode
<< 8)
293 | timers
->timer
[timer_nr
+1].mode
;
294 *(unsigned16
*)dest
= val16
;
298 hw_abort (me
, "bad read size of 2 bytes to TM%dMD.", timer_nr
);
305 val32
= (timers
->timer
[0].mode
<< 24 )
306 | (timers
->timer
[1].mode
<< 16)
307 | (timers
->timer
[2].mode
<< 8)
308 | timers
->timer
[3].mode
;
309 *(unsigned32
*)dest
= val32
;
313 hw_abort (me
, "bad read size of 4 bytes to TM%dMD.", timer_nr
);
318 hw_abort (me
, "bad read size of %d bytes to TM%dMD.",
325 read_base_reg (struct hw
*me
,
326 struct mn103tim
*timers
,
334 /* Check nr_bytes: accesses of 1, 2 and 4 bytes allowed depending on timer. */
338 /* Reading 1 byte is ok for all registers. */
339 if ( timer_nr
< NR_8BIT_TIMERS
)
341 *(unsigned8
*)dest
= timers
->timer
[timer_nr
].base
;
346 if ( timer_nr
== 1 || timer_nr
== 3 )
348 hw_abort (me
, "bad read size of 2 bytes to TM%dBR.", timer_nr
);
352 if ( timer_nr
< NR_8BIT_TIMERS
)
354 val16
= (timers
->timer
[timer_nr
].base
<<8)
355 | timers
->timer
[timer_nr
+1].base
;
359 val16
= timers
->timer
[timer_nr
].base
;
361 *(unsigned16
*)dest
= val16
;
368 val32
= (timers
->timer
[0].base
<< 24) | (timers
->timer
[1].base
<< 16)
369 | (timers
->timer
[2].base
<< 8) | timers
->timer
[3].base
;
370 *(unsigned32
*)dest
= val32
;
372 else if ( timer_nr
== 4 )
374 val32
= (timers
->timer
[4].base
<< 16) | timers
->timer
[5].base
;
375 *(unsigned32
*)dest
= val32
;
379 hw_abort (me
, "bad read size of 4 bytes to TM%dBR.", timer_nr
);
384 hw_abort (me
, "bad read size must of %d bytes to TM%dBR.",
391 read_counter (struct hw
*me
,
392 struct mn103tim
*timers
,
399 if ( NULL
== timers
->timer
[timer_nr
].event
)
401 /* Timer is not counting, use value in base register. */
402 val
= timers
->timer
[timer_nr
].base
;
406 /* ticks left = start time + div ratio - curr time */
407 /* Cannot use base register because it can be written during counting and it
408 doesn't affect counter until underflow occurs. */
410 val
= timers
->timer
[timer_nr
].start
+ timers
->timer
[timer_nr
].div_ratio
411 - hw_event_queue_time(me
);
416 *(unsigned8
*)dest
= val
;
420 *(unsigned16
*)dest
= val
;
424 *(unsigned32
*)dest
= val
;
428 hw_abort(me
, "bad read size for reading counter");
435 mn103tim_io_read_buffer (struct hw
*me
,
441 struct mn103tim
*timers
= hw_data (me
);
442 enum timer_register_types timer_reg
;
444 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
446 timer_reg
= decode_addr (me
, timers
, base
);
448 /* It can be either a mode register, a base register or a binary counter. */
449 /* Check in that order. */
450 if ( timer_reg
>= FIRST_MODE_REG
&& timer_reg
<= LAST_MODE_REG
)
452 read_mode_reg(me
, timers
, timer_reg
-FIRST_MODE_REG
, dest
, nr_bytes
);
454 else if ( timer_reg
<= LAST_BASE_REG
)
456 read_base_reg(me
, timers
, timer_reg
-FIRST_BASE_REG
, dest
, nr_bytes
);
458 else if ( timer_reg
<= LAST_COUNTER
)
460 read_counter(me
, timers
, timer_reg
-FIRST_COUNTER
, dest
, nr_bytes
);
464 hw_abort(me
, "invalid timer register address.");
472 do_counter_event (struct hw
*me
,
475 struct mn103tim
*timers
= hw_data(me
);
476 int timer_nr
= (int) data
;
478 /* Check if counting is still enabled. */
479 if ( (timers
->timer
[timer_nr
].mode
& count_mask
) != 0 )
481 /* Generate an interrupt for the timer underflow (TIMERn_UFLOW). */
482 hw_port_event (me
, timer_nr
/*uflow_port[timer_nr]*/, 1 /* level */);
484 /* Schedule next timeout. */
486 timers
->timer
[timer_nr
].start
= hw_event_queue_time(me
);
487 /* FIX: Check if div_ ratio has changed and if it's now 0. */
488 timers
->timer
[timer_nr
].event
489 = hw_event_queue_schedule (me
, timers
->timer
[timer_nr
].div_ratio
,
490 do_counter_event
, (void *)timer_nr
);
496 write_base_reg (struct hw
*me
,
497 struct mn103tim
*timers
,
503 const unsigned8
*buf8
= source
;
504 const unsigned16
*buf16
= source
;
507 /* If TMnCNE == 0 (counting is off), writing to the base register
508 (TMnBR) causes a simultaneous write to the counter reg (TMnBC).
509 Else, the TMnBC is reloaded with the value from TMnBR when
510 underflow occurs. Since the counter register is not explicitly
511 maintained, this functionality is handled in read_counter. */
513 mode_val
= timers
->timer
[timer_nr
].mode
;
515 /* Check nr_bytes: write of 1, 2 or 4 bytes allowed depending on timer. */
519 /* Storing 1 byte is ok for all registers. */
520 timers
->timer
[timer_nr
].base
= buf8
[0];
524 if ( timer_nr
== 1 || timer_nr
== 3 )
526 hw_abort (me
, "bad write size of 2 bytes to TM%dBR.", timer_nr
);
530 if ( timer_nr
< NR_8BIT_TIMERS
)
532 timers
->timer
[timer_nr
].base
= buf8
[0];
533 timers
->timer
[timer_nr
+1].base
= buf8
[1];
537 timers
->timer
[timer_nr
].base
= buf16
[0];
546 timers
->timer
[0].base
= buf8
[0];
547 timers
->timer
[1].base
= buf8
[1];
548 timers
->timer
[2].base
= buf8
[2];
549 timers
->timer
[3].base
= buf8
[3];
551 else if ( timer_nr
== 4 )
553 timers
->timer
[4].base
= buf16
[0];
554 timers
->timer
[5].base
= buf16
[1];
558 hw_abort (me
, "bad write size of 4 bytes to TM%dBR.", timer_nr
);
563 hw_abort (me
, "bad write size must of %d bytes to TM%dBR.",
570 write_8bit_mode_reg (struct hw
*me
,
571 struct mn103tim
*timers
,
575 /* for timers 0 to 3 */
578 unsigned8 mode_val
, next_mode_val
;
579 unsigned32 div_ratio
;
583 hw_abort (me
, "bad write size of %d bytes to TM%dMD.", nr_bytes
, timer_nr
);
586 mode_val
= *(unsigned8
*)source
;
587 timers
->timer
[timer_nr
].mode
= mode_val
;
589 if ( ( mode_val
& count_and_load_mask
) == count_and_load_mask
)
591 hw_abort(me
, "Cannot load base reg and start counting simultaneously.");
593 if ( ( mode_val
& bits2to5_mask
) != 0 )
595 hw_abort(me
, "Cannot write to bits 2 to 5 of mode register");
598 if ( mode_val
& count_mask
)
600 /* - de-schedule any previous event. */
601 /* - add new event to queue to start counting. */
602 /* - assert that counter == base reg? */
604 /* For cascaded timers, */
605 if ( (mode_val
& clock_mask
) == clk_cascaded
)
609 hw_abort(me
, "Timer 0 cannot be cascaded.");
614 div_ratio
= timers
->timer
[timer_nr
].base
;
616 /* Check for cascading. */
617 next_mode_val
= timers
->timer
[timer_nr
+1].mode
;
618 if ( ( next_mode_val
& clock_mask
) == clk_cascaded
)
620 /* Check that CNE is on. */
621 if ( ( next_mode_val
& count_mask
) == 0 )
623 hw_abort (me
, "cascaded timer not ready for counting");
625 ASSERT(timers
->timer
[timer_nr
+1].event
== NULL
);
626 ASSERT(timers
->timer
[timer_nr
+1].div_ratio
== 0);
627 div_ratio
= div_ratio
| (timers
->timer
[timer_nr
+1].base
<< 8);
630 timers
->timer
[timer_nr
].div_ratio
= div_ratio
;
632 if ( NULL
!= timers
->timer
[timer_nr
].event
)
634 hw_event_queue_deschedule (me
, timers
->timer
[timer_nr
].event
);
635 timers
->timer
[timer_nr
].event
= NULL
;
640 /* Set start time. */
641 timers
->timer
[timer_nr
].start
= hw_event_queue_time(me
);
643 timers
->timer
[timer_nr
].event
644 = hw_event_queue_schedule(me
, div_ratio
,
652 /* Turn off counting */
653 if ( NULL
!= timers
->timer
[timer_nr
].event
)
655 ASSERT((timers
->timer
[timer_nr
].mode
& clock_mask
) != clk_cascaded
);
656 hw_event_queue_deschedule (me
, timers
->timer
[timer_nr
].event
);
657 timers
->timer
[timer_nr
].event
= NULL
;
661 if ( (timers
->timer
[timer_nr
].mode
& clock_mask
) == clk_cascaded
)
663 ASSERT(timers
->timer
[timer_nr
].event
== NULL
);
672 write_16bit_mode_reg (struct hw
*me
,
673 struct mn103tim
*timers
,
677 /* for timers 4 and 5, not 6 */
680 unsigned8 mode_val
, next_mode_val
;
681 unsigned32 div_ratio
;
685 hw_abort (me
, "bad write size of %d bytes to TM%dMD.", nr_bytes
, timer_nr
);
688 mode_val
= *(unsigned8
*)source
;
689 timers
->timer
[timer_nr
].mode
= mode_val
;
691 if ( ( mode_val
& count_and_load_mask
) == count_and_load_mask
)
693 hw_abort(me
, "Cannot load base reg and start counting simultaneously.");
695 if ( ( mode_val
& bits2to5_mask
) != 0 )
697 hw_abort(me
, "Cannot write to bits 2 to 5 of mode register");
701 if ( mode_val
& count_mask
)
703 /* - de-schedule any previous event. */
704 /* - add new event to queue to start counting. */
705 /* - assert that counter == base reg? */
707 /* For cascaded timers, */
708 if ( (mode_val
& clock_mask
) == clk_cascaded
)
712 hw_abort(me
, "Timer 4 cannot be cascaded.");
717 div_ratio
= timers
->timer
[timer_nr
].base
;
719 /* Check for cascading. */
720 next_mode_val
= timers
->timer
[timer_nr
+1].mode
;
721 if ( ( next_mode_val
& clock_mask
) == clk_cascaded
)
723 /* Check that CNE is on. */
724 if ( ( next_mode_val
& count_mask
) == 0 )
726 hw_abort (me
, "cascaded timer not ready for counting");
728 ASSERT(timers
->timer
[timer_nr
+1].event
== NULL
);
729 ASSERT(timers
->timer
[timer_nr
+1].div_ratio
== 0);
730 div_ratio
= div_ratio
| (timers
->timer
[timer_nr
+1].base
<< 16);
733 timers
->timer
[timer_nr
].div_ratio
= div_ratio
;
735 if ( NULL
!= timers
->timer
[timer_nr
].event
)
737 hw_event_queue_deschedule (me
, timers
->timer
[timer_nr
].event
);
738 timers
->timer
[timer_nr
].event
= NULL
;
743 /* Set start time. */
744 timers
->timer
[timer_nr
].start
= hw_event_queue_time(me
);
746 timers
->timer
[timer_nr
].event
747 = hw_event_queue_schedule(me
, div_ratio
, do_counter_event
,
754 /* Turn off counting */
755 if ( NULL
!= timers
->timer
[timer_nr
].event
)
757 ASSERT((timers
->timer
[timer_nr
].mode
& clock_mask
) != clk_cascaded
);
758 hw_event_queue_deschedule (me
, timers
->timer
[timer_nr
].event
);
759 timers
->timer
[timer_nr
].event
= NULL
;
763 if ( (timers
->timer
[timer_nr
].mode
& clock_mask
) == clk_cascaded
)
765 ASSERT(timers
->timer
[timer_nr
].event
== NULL
);
774 mn103tim_io_write_buffer (struct hw
*me
,
780 struct mn103tim
*timers
= hw_data (me
);
781 enum timer_register_types timer_reg
;
783 HW_TRACE ((me
, "write to 0x%08lx length %d with 0x%x", (long) base
,
784 (int) nr_bytes
, *(unsigned32
*)source
));
786 timer_reg
= decode_addr (me
, timers
, base
);
788 /* It can be either a mode register, a base register or a binary counter. */
789 /* Check in that order. */
790 if ( timer_reg
<= LAST_MODE_REG
)
794 write_16bit_mode_reg(me
, timers
, timer_reg
-FIRST_MODE_REG
,
799 write_8bit_mode_reg(me
, timers
, timer_reg
-FIRST_MODE_REG
,
803 else if ( timer_reg
<= LAST_BASE_REG
)
805 write_base_reg(me
, timers
, timer_reg
-FIRST_BASE_REG
, source
, nr_bytes
);
807 else if ( timer_reg
<= LAST_COUNTER
)
809 hw_abort(me
, "cannot write to counter");
813 hw_abort(me
, "invalid reg type");
820 const struct hw_descriptor dv_mn103tim_descriptor
[] = {
821 { "mn103tim", mn103tim_finish
, },