1 /* This file is part of the program GDB, the GNU debugger.
3 Copyright (C) 1998-2019 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/>.
23 #include "dv-sockser.h"
29 mn103ser - mn103002 serial devices 0, 1 and 2.
34 Implements the mn103002 serial interfaces as described in the
40 reg = <serial-addr> <serial-size>
48 /* The serial devices' registers' address block */
50 struct mn103ser_block
{
57 enum serial_register_types
{
77 #define NR_SERIAL_DEVS 3
78 #define SIO_STAT_RRDY 0x0010
80 typedef struct _mn10300_serial
{
81 unsigned16 status
, control
;
82 unsigned8 txb
, rxb
, intmode
;
83 struct hw_event
*event
;
89 struct mn103ser_block block
;
90 mn10300_serial device
[NR_SERIAL_DEVS
];
91 unsigned8 serial2_timer_reg
;
92 do_hw_poll_read_method
*reader
;
95 /* output port ID's */
108 static const struct hw_port_descriptor mn103ser_ports
[] = {
110 { "serial-0-receive", SERIAL0_RECEIVE
, 0, output_port
, },
111 { "serial-1-receive", SERIAL1_RECEIVE
, 0, output_port
, },
112 { "serial-2-receive", SERIAL2_RECEIVE
, 0, output_port
, },
113 { "serial-0-transmit", SERIAL0_SEND
, 0, output_port
, },
114 { "serial-1-transmit", SERIAL1_SEND
, 0, output_port
, },
115 { "serial-2-transmit", SERIAL2_SEND
, 0, output_port
, },
122 /* Finish off the partially created hw device. Attach our local
123 callbacks. Wire up our port names etc */
125 static hw_io_read_buffer_method mn103ser_io_read_buffer
;
126 static hw_io_write_buffer_method mn103ser_io_write_buffer
;
129 attach_mn103ser_regs (struct hw
*me
,
130 struct mn103ser
*serial
)
132 unsigned_word attach_address
;
134 unsigned attach_size
;
135 reg_property_spec reg
;
137 if (hw_find_property (me
, "reg") == NULL
)
138 hw_abort (me
, "Missing \"reg\" property");
140 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
141 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
142 hw_unit_address_to_attach_address (hw_parent (me
),
147 serial
->block
.base
= attach_address
;
148 hw_unit_size_to_attach_size (hw_parent (me
),
151 serial
->block
.bound
= attach_address
+ (attach_size
- 1);
152 hw_attach_address (hw_parent (me
),
154 attach_space
, attach_address
, attach_size
,
159 mn103ser_finish (struct hw
*me
)
161 struct mn103ser
*serial
;
164 serial
= HW_ZALLOC (me
, struct mn103ser
);
165 set_hw_data (me
, serial
);
166 set_hw_io_read_buffer (me
, mn103ser_io_read_buffer
);
167 set_hw_io_write_buffer (me
, mn103ser_io_write_buffer
);
168 set_hw_ports (me
, mn103ser_ports
);
170 /* Attach ourself to our parent bus */
171 attach_mn103ser_regs (me
, serial
);
173 /* If so configured, enable polled input */
174 if (hw_find_property (me
, "poll?") != NULL
175 && hw_find_boolean_property (me
, "poll?"))
177 serial
->reader
= sim_io_poll_read
;
181 serial
->reader
= sim_io_read
;
184 /* Initialize the serial device registers. */
185 for ( i
=0; i
<NR_SERIAL_DEVS
; ++i
)
187 serial
->device
[i
].txb
= 0;
188 serial
->device
[i
].rxb
= 0;
189 serial
->device
[i
].status
= 0;
190 serial
->device
[i
].control
= 0;
191 serial
->device
[i
].intmode
= 0;
192 serial
->device
[i
].event
= NULL
;
200 decode_addr (struct hw
*me
,
201 struct mn103ser
*serial
,
202 unsigned_word address
)
204 unsigned_word offset
;
205 offset
= address
- serial
->block
.base
;
208 case 0x00: return SC0CTR
;
209 case 0x04: return SC0ICR
;
210 case 0x08: return SC0TXB
;
211 case 0x09: return SC0RXB
;
212 case 0x0C: return SC0STR
;
213 case 0x10: return SC1CTR
;
214 case 0x14: return SC1ICR
;
215 case 0x18: return SC1TXB
;
216 case 0x19: return SC1RXB
;
217 case 0x1C: return SC1STR
;
218 case 0x20: return SC2CTR
;
219 case 0x24: return SC2ICR
;
220 case 0x28: return SC2TXB
;
221 case 0x29: return SC2RXB
;
222 case 0x2C: return SC2STR
;
223 case 0x2D: return SC2TIM
;
226 hw_abort (me
, "bad address");
233 do_polling_event (struct hw
*me
,
236 SIM_DESC sd
= hw_system (me
);
237 struct mn103ser
*serial
= hw_data(me
);
238 long serial_reg
= (long) data
;
242 status
= dv_sockser_status (sd
);
243 if (!(status
& DV_SOCKSER_DISCONNECTED
))
246 rd
= dv_sockser_read (sd
);
254 count
= HW_IO_NOT_READY
;
259 count
= do_hw_poll_read (me
, serial
->reader
,
260 0/*STDIN*/, &c
, sizeof(c
));
266 case HW_IO_NOT_READY
:
268 serial
->device
[serial_reg
].rxb
= 0;
269 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
272 serial
->device
[serial_reg
].rxb
= c
;
273 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
274 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
277 /* Schedule next polling event */
278 serial
->device
[serial_reg
].event
279 = hw_event_queue_schedule (me
, 1000,
280 do_polling_event
, (void *)serial_reg
);
285 read_control_reg (struct hw
*me
,
286 struct mn103ser
*serial
,
287 unsigned_word serial_reg
,
291 /* really allow 1 byte read, too */
294 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].control
);
298 hw_abort (me
, "bad read size of %d bytes from SC%dCTR.", nr_bytes
,
305 read_intmode_reg (struct hw
*me
,
306 struct mn103ser
*serial
,
307 unsigned_word serial_reg
,
313 *(unsigned8
*)dest
= serial
->device
[serial_reg
].intmode
;
317 hw_abort (me
, "bad read size of %d bytes from SC%dICR.", nr_bytes
,
324 read_txb (struct hw
*me
,
325 struct mn103ser
*serial
,
326 unsigned_word serial_reg
,
332 *(unsigned8
*)dest
= serial
->device
[serial_reg
].txb
;
336 hw_abort (me
, "bad read size of %d bytes from SC%dTXB.", nr_bytes
,
343 read_rxb (struct hw
*me
,
344 struct mn103ser
*serial
,
345 unsigned_word serial_reg
,
351 *(unsigned8
*)dest
= serial
->device
[serial_reg
].rxb
;
352 /* Reception buffer is now empty. */
353 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
357 hw_abort (me
, "bad read size of %d bytes from SC%dRXB.", nr_bytes
,
364 read_status_reg (struct hw
*me
,
365 struct mn103ser
*serial
,
366 unsigned_word serial_reg
,
373 if ( (serial
->device
[serial_reg
].status
& SIO_STAT_RRDY
) == 0 )
375 SIM_DESC sd
= hw_system (me
);
379 /* Kill current poll event */
380 if ( NULL
!= serial
->device
[serial_reg
].event
)
382 hw_event_queue_deschedule (me
, serial
->device
[serial_reg
].event
);
383 serial
->device
[serial_reg
].event
= NULL
;
386 status
= dv_sockser_status (sd
);
387 if (!(status
& DV_SOCKSER_DISCONNECTED
))
390 rd
= dv_sockser_read (sd
);
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,
424 (void *) (long) serial_reg
);
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 SIM_DESC sd
= hw_system (me
);
604 serial
->device
[serial_reg
].txb
= *(unsigned8
*)source
;
606 status
= dv_sockser_status (sd
);
607 if (!(status
& DV_SOCKSER_DISCONNECTED
))
609 dv_sockser_write(sd
, * (char*) source
);
613 sim_io_write_stdout(sd
, (char *)source
, 1);
614 sim_io_flush_stdout(sd
);
617 hw_port_event (me
, serial_reg
+SERIAL0_SEND
, 1);
621 hw_abort (me
, "bad write size of %d bytes to SC%dTXB.", nr_bytes
,
628 write_serial2_timer_reg (struct hw
*me
,
629 struct mn103ser
*serial
,
635 serial
->serial2_timer_reg
= *(unsigned8
*)source
;
639 hw_abort (me
, "bad write size of %d bytes to SC2TIM.", nr_bytes
);
645 mn103ser_io_write_buffer (struct hw
*me
,
651 struct mn103ser
*serial
= hw_data (me
);
652 enum serial_register_types serial_reg
;
653 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
655 serial_reg
= decode_addr (me
, serial
, base
);
658 /* control registers */
662 HW_TRACE ((me
, "write - ctrl reg%d has 0x%x, nrbytes=%d.\n",
663 serial_reg
-SC0CTR
, *(unsigned8
*)source
, nr_bytes
));
664 write_control_reg(me
, serial
, serial_reg
-SC0CTR
, source
, nr_bytes
);
667 /* interrupt mode registers */
671 HW_TRACE ((me
, "write - intmode reg%d has 0x%x, nrbytes=%d.\n",
672 serial_reg
-SC0ICR
, *(unsigned8
*)source
, nr_bytes
));
673 write_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, source
, nr_bytes
);
676 /* transmission buffers */
680 HW_TRACE ((me
, "write - txb%d has %c, nrbytes=%d.\n",
681 serial_reg
-SC0TXB
, *(char *)source
, nr_bytes
));
682 write_txb(me
, serial
, serial_reg
-SC0TXB
, source
, nr_bytes
);
685 /* reception buffers */
689 hw_abort(me
, "Cannot write to reception buffer.");
692 /* status registers */
696 hw_abort(me
, "Cannot write to status register.");
700 HW_TRACE ((me
, "read - serial2 timer reg %d (nrbytes=%d)\n",
701 *(unsigned8
*)source
, nr_bytes
));
702 write_serial2_timer_reg(me
, serial
, source
, nr_bytes
);
706 hw_abort(me
, "invalid address");
713 const struct hw_descriptor dv_mn103ser_descriptor
[] = {
714 { "mn103ser", mn103ser_finish
, },