1 /* This file is part of the program GDB, the GNU debugger.
3 Copyright (C) 1998, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 #define NR_SERIAL_DEVS 3
79 #define SIO_STAT_RRDY 0x0010
81 typedef struct _mn10300_serial
{
82 unsigned16 status
, control
;
83 unsigned8 txb
, rxb
, intmode
;
84 struct hw_event
*event
;
90 struct mn103ser_block block
;
91 mn10300_serial device
[NR_SERIAL_DEVS
];
92 unsigned8 serial2_timer_reg
;
93 do_hw_poll_read_method
*reader
;
96 /* output port ID's */
109 static const struct hw_port_descriptor mn103ser_ports
[] = {
111 { "serial-0-receive", SERIAL0_RECEIVE
, 0, output_port
, },
112 { "serial-1-receive", SERIAL1_RECEIVE
, 0, output_port
, },
113 { "serial-2-receive", SERIAL2_RECEIVE
, 0, output_port
, },
114 { "serial-0-transmit", SERIAL0_SEND
, 0, output_port
, },
115 { "serial-1-transmit", SERIAL1_SEND
, 0, output_port
, },
116 { "serial-2-transmit", SERIAL2_SEND
, 0, output_port
, },
123 /* Finish off the partially created hw device. Attach our local
124 callbacks. Wire up our port names etc */
126 static hw_io_read_buffer_method mn103ser_io_read_buffer
;
127 static hw_io_write_buffer_method mn103ser_io_write_buffer
;
130 attach_mn103ser_regs (struct hw
*me
,
131 struct mn103ser
*serial
)
133 unsigned_word attach_address
;
135 unsigned attach_size
;
136 reg_property_spec reg
;
138 if (hw_find_property (me
, "reg") == NULL
)
139 hw_abort (me
, "Missing \"reg\" property");
141 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
142 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
143 hw_unit_address_to_attach_address (hw_parent (me
),
148 serial
->block
.base
= attach_address
;
149 hw_unit_size_to_attach_size (hw_parent (me
),
152 serial
->block
.bound
= attach_address
+ (attach_size
- 1);
153 hw_attach_address (hw_parent (me
),
155 attach_space
, attach_address
, attach_size
,
160 mn103ser_finish (struct hw
*me
)
162 struct mn103ser
*serial
;
165 serial
= HW_ZALLOC (me
, struct mn103ser
);
166 set_hw_data (me
, serial
);
167 set_hw_io_read_buffer (me
, mn103ser_io_read_buffer
);
168 set_hw_io_write_buffer (me
, mn103ser_io_write_buffer
);
169 set_hw_ports (me
, mn103ser_ports
);
171 /* Attach ourself to our parent bus */
172 attach_mn103ser_regs (me
, serial
);
174 /* If so configured, enable polled input */
175 if (hw_find_property (me
, "poll?") != NULL
176 && hw_find_boolean_property (me
, "poll?"))
178 serial
->reader
= sim_io_poll_read
;
182 serial
->reader
= sim_io_read
;
185 /* Initialize the serial device registers. */
186 for ( i
=0; i
<NR_SERIAL_DEVS
; ++i
)
188 serial
->device
[i
].txb
= 0;
189 serial
->device
[i
].rxb
= 0;
190 serial
->device
[i
].status
= 0;
191 serial
->device
[i
].control
= 0;
192 serial
->device
[i
].intmode
= 0;
193 serial
->device
[i
].event
= NULL
;
201 decode_addr (struct hw
*me
,
202 struct mn103ser
*serial
,
203 unsigned_word address
)
205 unsigned_word offset
;
206 offset
= address
- serial
->block
.base
;
209 case 0x00: return SC0CTR
;
210 case 0x04: return SC0ICR
;
211 case 0x08: return SC0TXB
;
212 case 0x09: return SC0RXB
;
213 case 0x0C: return SC0STR
;
214 case 0x10: return SC1CTR
;
215 case 0x14: return SC1ICR
;
216 case 0x18: return SC1TXB
;
217 case 0x19: return SC1RXB
;
218 case 0x1C: return SC1STR
;
219 case 0x20: return SC2CTR
;
220 case 0x24: return SC2ICR
;
221 case 0x28: return SC2TXB
;
222 case 0x29: return SC2RXB
;
223 case 0x2C: return SC2STR
;
224 case 0x2D: return SC2TIM
;
227 hw_abort (me
, "bad address");
234 do_polling_event (struct hw
*me
,
237 SIM_DESC sd
= hw_system (me
);
238 struct mn103ser
*serial
= hw_data(me
);
239 long serial_reg
= (long) data
;
243 status
= dv_sockser_status (sd
);
244 if (!(status
& DV_SOCKSER_DISCONNECTED
))
247 rd
= dv_sockser_read (sd
);
255 count
= HW_IO_NOT_READY
;
260 count
= do_hw_poll_read (me
, serial
->reader
,
261 0/*STDIN*/, &c
, sizeof(c
));
267 case HW_IO_NOT_READY
:
269 serial
->device
[serial_reg
].rxb
= 0;
270 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
273 serial
->device
[serial_reg
].rxb
= c
;
274 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
275 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
278 /* Schedule next polling event */
279 serial
->device
[serial_reg
].event
280 = hw_event_queue_schedule (me
, 1000,
281 do_polling_event
, (void *)serial_reg
);
286 read_control_reg (struct hw
*me
,
287 struct mn103ser
*serial
,
288 unsigned_word serial_reg
,
292 /* really allow 1 byte read, too */
295 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].control
);
299 hw_abort (me
, "bad read size of %d bytes from SC%dCTR.", nr_bytes
,
306 read_intmode_reg (struct hw
*me
,
307 struct mn103ser
*serial
,
308 unsigned_word serial_reg
,
314 *(unsigned8
*)dest
= serial
->device
[serial_reg
].intmode
;
318 hw_abort (me
, "bad read size of %d bytes from SC%dICR.", nr_bytes
,
325 read_txb (struct hw
*me
,
326 struct mn103ser
*serial
,
327 unsigned_word serial_reg
,
333 *(unsigned8
*)dest
= serial
->device
[serial_reg
].txb
;
337 hw_abort (me
, "bad read size of %d bytes from SC%dTXB.", nr_bytes
,
344 read_rxb (struct hw
*me
,
345 struct mn103ser
*serial
,
346 unsigned_word serial_reg
,
352 *(unsigned8
*)dest
= serial
->device
[serial_reg
].rxb
;
353 /* Reception buffer is now empty. */
354 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
358 hw_abort (me
, "bad read size of %d bytes from SC%dRXB.", nr_bytes
,
365 read_status_reg (struct hw
*me
,
366 struct mn103ser
*serial
,
367 unsigned_word serial_reg
,
374 if ( (serial
->device
[serial_reg
].status
& SIO_STAT_RRDY
) == 0 )
376 SIM_DESC sd
= hw_system (me
);
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
;
387 status
= dv_sockser_status (sd
);
388 if (!(status
& DV_SOCKSER_DISCONNECTED
))
391 rd
= dv_sockser_read (sd
);
399 count
= HW_IO_NOT_READY
;
404 count
= do_hw_poll_read (me
, serial
->reader
,
405 0/*STDIN*/, &c
, sizeof(c
));
410 case HW_IO_NOT_READY
:
412 serial
->device
[serial_reg
].rxb
= 0;
413 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
416 serial
->device
[serial_reg
].rxb
= c
;
417 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
418 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
421 /* schedule polling event */
422 serial
->device
[serial_reg
].event
423 = hw_event_queue_schedule (me
, 1000,
425 (void *) (long) serial_reg
);
430 *(unsigned8
*)dest
= (unsigned8
)serial
->device
[serial_reg
].status
;
432 else if ( nr_bytes
== 2 && serial_reg
!= SC2STR
)
434 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].status
);
438 hw_abort (me
, "bad read size of %d bytes from SC%dSTR.", nr_bytes
,
445 read_serial2_timer_reg (struct hw
*me
,
446 struct mn103ser
*serial
,
452 * (unsigned8
*) dest
= (unsigned8
) serial
->serial2_timer_reg
;
456 hw_abort (me
, "bad read size of %d bytes to SC2TIM.", nr_bytes
);
462 mn103ser_io_read_buffer (struct hw
*me
,
468 struct mn103ser
*serial
= hw_data (me
);
469 enum serial_register_types serial_reg
;
470 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
472 serial_reg
= decode_addr (me
, serial
, base
);
475 /* control registers */
479 read_control_reg(me
, serial
, serial_reg
-SC0CTR
, dest
, nr_bytes
);
480 HW_TRACE ((me
, "read - ctrl reg%d has 0x%x\n", serial_reg
-SC0CTR
,
481 *(unsigned8
*)dest
));
484 /* interrupt mode registers */
488 read_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, dest
, nr_bytes
);
489 HW_TRACE ((me
, "read - intmode reg%d has 0x%x\n", serial_reg
-SC0ICR
,
490 *(unsigned8
*)dest
));
493 /* transmission buffers */
497 read_txb(me
, serial
, serial_reg
-SC0TXB
, dest
, nr_bytes
);
498 HW_TRACE ((me
, "read - txb%d has %c\n", serial_reg
-SC0TXB
,
502 /* reception buffers */
506 read_rxb(me
, serial
, serial_reg
-SC0RXB
, dest
, nr_bytes
);
507 HW_TRACE ((me
, "read - rxb%d has %c\n", serial_reg
-SC0RXB
,
511 /* status registers */
515 read_status_reg(me
, serial
, serial_reg
-SC0STR
, dest
, nr_bytes
);
516 HW_TRACE ((me
, "read - status reg%d has 0x%x\n", serial_reg
-SC0STR
,
517 *(unsigned8
*)dest
));
521 read_serial2_timer_reg(me
, serial
, dest
, nr_bytes
);
522 HW_TRACE ((me
, "read - serial2 timer reg %d\n", *(unsigned8
*)dest
));
526 hw_abort(me
, "invalid address");
534 write_control_reg (struct hw
*me
,
535 struct mn103ser
*serial
,
536 unsigned_word serial_reg
,
540 unsigned16 val
= LE2H_2 (*(unsigned16
*)source
);
542 /* really allow 1 byte write, too */
545 if ( serial_reg
== 2 && (val
& 0x0C04) != 0 )
547 hw_abort(me
, "Cannot write to read-only bits of SC2CTR.");
551 serial
->device
[serial_reg
].control
= val
;
556 hw_abort (me
, "bad read size of %d bytes from SC%dSTR.", nr_bytes
,
563 write_intmode_reg (struct hw
*me
,
564 struct mn103ser
*serial
,
565 unsigned_word serial_reg
,
569 unsigned8 val
= *(unsigned8
*)source
;
573 /* Check for attempt to write to read-only bits of register. */
574 if ( ( serial_reg
== 2 && (val
& 0xCA) != 0 )
575 || ( serial_reg
!= 2 && (val
& 0x4A) != 0 ) )
577 hw_abort(me
, "Cannot write to read-only bits of SC%dICR.",
582 serial
->device
[serial_reg
].intmode
= val
;
587 hw_abort (me
, "bad write size of %d bytes to SC%dICR.", nr_bytes
,
594 write_txb (struct hw
*me
,
595 struct mn103ser
*serial
,
596 unsigned_word serial_reg
,
602 SIM_DESC sd
= hw_system (me
);
605 serial
->device
[serial_reg
].txb
= *(unsigned8
*)source
;
607 status
= dv_sockser_status (sd
);
608 if (!(status
& DV_SOCKSER_DISCONNECTED
))
610 dv_sockser_write(sd
, * (char*) source
);
614 sim_io_write_stdout(sd
, (char *)source
, 1);
615 sim_io_flush_stdout(sd
);
618 hw_port_event (me
, serial_reg
+SERIAL0_SEND
, 1);
622 hw_abort (me
, "bad write size of %d bytes to SC%dTXB.", nr_bytes
,
629 write_serial2_timer_reg (struct hw
*me
,
630 struct mn103ser
*serial
,
636 serial
->serial2_timer_reg
= *(unsigned8
*)source
;
640 hw_abort (me
, "bad write size of %d bytes to SC2TIM.", nr_bytes
);
646 mn103ser_io_write_buffer (struct hw
*me
,
652 struct mn103ser
*serial
= hw_data (me
);
653 enum serial_register_types serial_reg
;
654 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
656 serial_reg
= decode_addr (me
, serial
, base
);
659 /* control registers */
663 HW_TRACE ((me
, "write - ctrl reg%d has 0x%x, nrbytes=%d.\n",
664 serial_reg
-SC0CTR
, *(unsigned8
*)source
, nr_bytes
));
665 write_control_reg(me
, serial
, serial_reg
-SC0CTR
, source
, nr_bytes
);
668 /* interrupt mode registers */
672 HW_TRACE ((me
, "write - intmode reg%d has 0x%x, nrbytes=%d.\n",
673 serial_reg
-SC0ICR
, *(unsigned8
*)source
, nr_bytes
));
674 write_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, source
, nr_bytes
);
677 /* transmission buffers */
681 HW_TRACE ((me
, "write - txb%d has %c, nrbytes=%d.\n",
682 serial_reg
-SC0TXB
, *(char *)source
, nr_bytes
));
683 write_txb(me
, serial
, serial_reg
-SC0TXB
, source
, nr_bytes
);
686 /* reception buffers */
690 hw_abort(me
, "Cannot write to reception buffer.");
693 /* status registers */
697 hw_abort(me
, "Cannot write to status register.");
701 HW_TRACE ((me
, "read - serial2 timer reg %d (nrbytes=%d)\n",
702 *(unsigned8
*)source
, nr_bytes
));
703 write_serial2_timer_reg(me
, serial
, source
, nr_bytes
);
707 hw_abort(me
, "invalid address");
714 const struct hw_descriptor dv_mn103ser_descriptor
[] = {
715 { "mn103ser", mn103ser_finish
, },