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.
24 #include "dv-sockser.h"
30 mn103ser - mn103002 serial devices 0, 1 and 2.
35 Implements the mn103002 serial interfaces as described in the
41 reg = <serial-addr> <serial-size>
49 /* The serial devices' registers' address block */
51 struct mn103ser_block
{
58 enum serial_register_types
{
78 /* Access dv-sockser state */
79 extern char* sockser_addr
;
80 #define USE_SOCKSER_P (sockser_addr != NULL)
83 #define NR_SERIAL_DEVS 3
84 #define SIO_STAT_RRDY 0x0010
86 typedef struct _mn10300_serial
{
87 unsigned16 status
, control
;
88 unsigned8 txb
, rxb
, intmode
;
89 struct hw_event
*event
;
95 struct mn103ser_block block
;
96 mn10300_serial device
[NR_SERIAL_DEVS
];
97 unsigned8 serial2_timer_reg
;
98 do_hw_poll_read_method
*reader
;
101 /* output port ID's */
114 static const struct hw_port_descriptor mn103ser_ports
[] = {
116 { "serial-0-receive", SERIAL0_RECEIVE
, 0, output_port
, },
117 { "serial-1-receive", SERIAL1_RECEIVE
, 0, output_port
, },
118 { "serial-2-receive", SERIAL2_RECEIVE
, 0, output_port
, },
119 { "serial-0-transmit", SERIAL0_SEND
, 0, output_port
, },
120 { "serial-1-transmit", SERIAL1_SEND
, 0, output_port
, },
121 { "serial-2-transmit", SERIAL2_SEND
, 0, output_port
, },
128 /* Finish off the partially created hw device. Attach our local
129 callbacks. Wire up our port names etc */
131 static hw_io_read_buffer_method mn103ser_io_read_buffer
;
132 static hw_io_write_buffer_method mn103ser_io_write_buffer
;
135 attach_mn103ser_regs (struct hw
*me
,
136 struct mn103ser
*serial
)
138 unsigned_word attach_address
;
140 unsigned attach_size
;
141 reg_property_spec reg
;
143 if (hw_find_property (me
, "reg") == NULL
)
144 hw_abort (me
, "Missing \"reg\" property");
146 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
147 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
148 hw_unit_address_to_attach_address (hw_parent (me
),
153 serial
->block
.base
= attach_address
;
154 hw_unit_size_to_attach_size (hw_parent (me
),
157 serial
->block
.bound
= attach_address
+ (attach_size
- 1);
158 hw_attach_address (hw_parent (me
),
160 attach_space
, attach_address
, attach_size
,
165 mn103ser_finish (struct hw
*me
)
167 struct mn103ser
*serial
;
170 serial
= HW_ZALLOC (me
, struct mn103ser
);
171 set_hw_data (me
, serial
);
172 set_hw_io_read_buffer (me
, mn103ser_io_read_buffer
);
173 set_hw_io_write_buffer (me
, mn103ser_io_write_buffer
);
174 set_hw_ports (me
, mn103ser_ports
);
176 /* Attach ourself to our parent bus */
177 attach_mn103ser_regs (me
, serial
);
179 /* If so configured, enable polled input */
180 if (hw_find_property (me
, "poll?") != NULL
181 && hw_find_boolean_property (me
, "poll?"))
183 serial
->reader
= sim_io_poll_read
;
187 serial
->reader
= sim_io_read
;
190 /* Initialize the serial device registers. */
191 for ( i
=0; i
<NR_SERIAL_DEVS
; ++i
)
193 serial
->device
[i
].txb
= 0;
194 serial
->device
[i
].rxb
= 0;
195 serial
->device
[i
].status
= 0;
196 serial
->device
[i
].control
= 0;
197 serial
->device
[i
].intmode
= 0;
198 serial
->device
[i
].event
= NULL
;
206 decode_addr (struct hw
*me
,
207 struct mn103ser
*serial
,
208 unsigned_word address
)
210 unsigned_word offset
;
211 offset
= address
- serial
->block
.base
;
214 case 0x00: return SC0CTR
;
215 case 0x04: return SC0ICR
;
216 case 0x08: return SC0TXB
;
217 case 0x09: return SC0RXB
;
218 case 0x0C: return SC0STR
;
219 case 0x10: return SC1CTR
;
220 case 0x14: return SC1ICR
;
221 case 0x18: return SC1TXB
;
222 case 0x19: return SC1RXB
;
223 case 0x1C: return SC1STR
;
224 case 0x20: return SC2CTR
;
225 case 0x24: return SC2ICR
;
226 case 0x28: return SC2TXB
;
227 case 0x29: return SC2RXB
;
228 case 0x2C: return SC2STR
;
229 case 0x2D: return SC2TIM
;
232 hw_abort (me
, "bad address");
239 do_polling_event (struct hw
*me
,
242 struct mn103ser
*serial
= hw_data(me
);
243 int serial_reg
= (int) data
;
250 rd
= dv_sockser_read (hw_system (me
));
258 count
= HW_IO_NOT_READY
;
263 count
= do_hw_poll_read (me
, serial
->reader
,
264 0/*STDIN*/, &c
, sizeof(c
));
270 case HW_IO_NOT_READY
:
272 serial
->device
[serial_reg
].rxb
= 0;
273 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
276 serial
->device
[serial_reg
].rxb
= c
;
277 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
278 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
281 /* Schedule next polling event */
282 serial
->device
[serial_reg
].event
283 = hw_event_queue_schedule (me
, 1000,
284 do_polling_event
, (void *)serial_reg
);
289 read_control_reg (struct hw
*me
,
290 struct mn103ser
*serial
,
291 unsigned_word serial_reg
,
295 /* really allow 1 byte read, too */
298 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].control
);
302 hw_abort (me
, "bad read size of %d bytes from SC%dCTR.", nr_bytes
,
309 read_intmode_reg (struct hw
*me
,
310 struct mn103ser
*serial
,
311 unsigned_word serial_reg
,
317 *(unsigned8
*)dest
= serial
->device
[serial_reg
].intmode
;
321 hw_abort (me
, "bad read size of %d bytes from SC%dICR.", nr_bytes
,
328 read_txb (struct hw
*me
,
329 struct mn103ser
*serial
,
330 unsigned_word serial_reg
,
336 *(unsigned8
*)dest
= serial
->device
[serial_reg
].txb
;
340 hw_abort (me
, "bad read size of %d bytes from SC%dTXB.", nr_bytes
,
347 read_rxb (struct hw
*me
,
348 struct mn103ser
*serial
,
349 unsigned_word serial_reg
,
355 *(unsigned8
*)dest
= serial
->device
[serial_reg
].rxb
;
356 /* Reception buffer is now empty. */
357 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
361 hw_abort (me
, "bad read size of %d bytes from SC%dRXB.", nr_bytes
,
368 read_status_reg (struct hw
*me
,
369 struct mn103ser
*serial
,
370 unsigned_word serial_reg
,
377 if ( (serial
->device
[serial_reg
].status
& SIO_STAT_RRDY
) == 0 )
380 /* Kill current poll event */
381 if ( NULL
!= serial
->device
[serial_reg
].event
)
383 hw_event_queue_deschedule (me
, serial
->device
[serial_reg
].event
);
384 serial
->device
[serial_reg
].event
= NULL
;
390 rd
= dv_sockser_read (hw_system (me
));
398 count
= HW_IO_NOT_READY
;
403 count
= do_hw_poll_read (me
, serial
->reader
,
404 0/*STDIN*/, &c
, sizeof(c
));
409 case HW_IO_NOT_READY
:
411 serial
->device
[serial_reg
].rxb
= 0;
412 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
415 serial
->device
[serial_reg
].rxb
= c
;
416 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
417 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
420 /* schedule polling event */
421 serial
->device
[serial_reg
].event
422 = hw_event_queue_schedule (me
, 1000,
429 *(unsigned8
*)dest
= (unsigned8
)serial
->device
[serial_reg
].status
;
431 else if ( nr_bytes
== 2 && serial_reg
!= SC2STR
)
433 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].status
);
437 hw_abort (me
, "bad read size of %d bytes from SC%dSTR.", nr_bytes
,
444 read_serial2_timer_reg (struct hw
*me
,
445 struct mn103ser
*serial
,
451 * (unsigned8
*) dest
= (unsigned8
) serial
->serial2_timer_reg
;
455 hw_abort (me
, "bad read size of %d bytes to SC2TIM.", nr_bytes
);
461 mn103ser_io_read_buffer (struct hw
*me
,
467 struct mn103ser
*serial
= hw_data (me
);
468 enum serial_register_types serial_reg
;
469 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
471 serial_reg
= decode_addr (me
, serial
, base
);
474 /* control registers */
478 read_control_reg(me
, serial
, serial_reg
-SC0CTR
, dest
, nr_bytes
);
479 HW_TRACE ((me
, "read - ctrl reg%d has 0x%x\n", serial_reg
-SC0CTR
,
480 *(unsigned8
*)dest
));
483 /* interrupt mode registers */
487 read_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, dest
, nr_bytes
);
488 HW_TRACE ((me
, "read - intmode reg%d has 0x%x\n", serial_reg
-SC0ICR
,
489 *(unsigned8
*)dest
));
492 /* transmission buffers */
496 read_txb(me
, serial
, serial_reg
-SC0TXB
, dest
, nr_bytes
);
497 HW_TRACE ((me
, "read - txb%d has %c\n", serial_reg
-SC0TXB
,
501 /* reception buffers */
505 read_rxb(me
, serial
, serial_reg
-SC0RXB
, dest
, nr_bytes
);
506 HW_TRACE ((me
, "read - rxb%d has %c\n", serial_reg
-SC0RXB
,
510 /* status registers */
514 read_status_reg(me
, serial
, serial_reg
-SC0STR
, dest
, nr_bytes
);
515 HW_TRACE ((me
, "read - status reg%d has 0x%x\n", serial_reg
-SC0STR
,
516 *(unsigned8
*)dest
));
520 read_serial2_timer_reg(me
, serial
, dest
, nr_bytes
);
521 HW_TRACE ((me
, "read - serial2 timer reg %d\n", *(unsigned8
*)dest
));
525 hw_abort(me
, "invalid address");
533 write_control_reg (struct hw
*me
,
534 struct mn103ser
*serial
,
535 unsigned_word serial_reg
,
539 unsigned16 val
= LE2H_2 (*(unsigned16
*)source
);
541 /* really allow 1 byte write, too */
544 if ( serial_reg
== 2 && (val
& 0x0C04) != 0 )
546 hw_abort(me
, "Cannot write to read-only bits of SC2CTR.");
550 serial
->device
[serial_reg
].control
= val
;
555 hw_abort (me
, "bad read size of %d bytes from SC%dSTR.", nr_bytes
,
562 write_intmode_reg (struct hw
*me
,
563 struct mn103ser
*serial
,
564 unsigned_word serial_reg
,
568 unsigned8 val
= *(unsigned8
*)source
;
572 /* Check for attempt to write to read-only bits of register. */
573 if ( ( serial_reg
== 2 && (val
& 0xCA) != 0 )
574 || ( serial_reg
!= 2 && (val
& 0x4A) != 0 ) )
576 hw_abort(me
, "Cannot write to read-only bits of SC%dICR.",
581 serial
->device
[serial_reg
].intmode
= val
;
586 hw_abort (me
, "bad write size of %d bytes to SC%dICR.", nr_bytes
,
593 write_txb (struct hw
*me
,
594 struct mn103ser
*serial
,
595 unsigned_word serial_reg
,
601 serial
->device
[serial_reg
].txb
= *(unsigned8
*)source
;
605 dv_sockser_write(hw_system (me
), * (char*) source
);
609 sim_io_write_stdout(hw_system (me
), (char *)source
, 1);
610 sim_io_flush_stdout(hw_system (me
));
613 hw_port_event (me
, serial_reg
+SERIAL0_SEND
, 1);
617 hw_abort (me
, "bad write size of %d bytes to SC%dTXB.", nr_bytes
,
624 write_serial2_timer_reg (struct hw
*me
,
625 struct mn103ser
*serial
,
631 serial
->serial2_timer_reg
= *(unsigned8
*)source
;
635 hw_abort (me
, "bad write size of %d bytes to SC2TIM.", nr_bytes
);
641 mn103ser_io_write_buffer (struct hw
*me
,
647 struct mn103ser
*serial
= hw_data (me
);
648 enum serial_register_types serial_reg
;
649 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
651 serial_reg
= decode_addr (me
, serial
, base
);
654 /* control registers */
658 HW_TRACE ((me
, "write - ctrl reg%d has 0x%x, nrbytes=%d.\n",
659 serial_reg
-SC0CTR
, *(unsigned8
*)source
, nr_bytes
));
660 write_control_reg(me
, serial
, serial_reg
-SC0CTR
, source
, nr_bytes
);
663 /* interrupt mode registers */
667 HW_TRACE ((me
, "write - intmode reg%d has 0x%x, nrbytes=%d.\n",
668 serial_reg
-SC0ICR
, *(unsigned8
*)source
, nr_bytes
));
669 write_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, source
, nr_bytes
);
672 /* transmission buffers */
676 HW_TRACE ((me
, "write - txb%d has %c, nrbytes=%d.\n",
677 serial_reg
-SC0TXB
, *(char *)source
, nr_bytes
));
678 write_txb(me
, serial
, serial_reg
-SC0TXB
, source
, nr_bytes
);
681 /* reception buffers */
685 hw_abort(me
, "Cannot write to reception buffer.");
688 /* status registers */
692 hw_abort(me
, "Cannot write to status register.");
696 HW_TRACE ((me
, "read - serial2 timer reg %d (nrbytes=%d)\n",
697 *(unsigned8
*)source
, nr_bytes
));
698 write_serial2_timer_reg(me
, serial
, source
, nr_bytes
);
702 hw_abort(me
, "invalid address");
709 const struct hw_descriptor dv_mn103ser_descriptor
[] = {
710 { "mn103ser", mn103ser_finish
, },