1 /* This file is part of the program GDB, the GNU debugger.
3 Copyright (C) 1998, 2007, 2008, 2009, 2010 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 /* Access dv-sockser state */
78 extern char* sockser_addr
;
79 #define USE_SOCKSER_P (sockser_addr != NULL)
82 #define NR_SERIAL_DEVS 3
83 #define SIO_STAT_RRDY 0x0010
85 typedef struct _mn10300_serial
{
86 unsigned16 status
, control
;
87 unsigned8 txb
, rxb
, intmode
;
88 struct hw_event
*event
;
94 struct mn103ser_block block
;
95 mn10300_serial device
[NR_SERIAL_DEVS
];
96 unsigned8 serial2_timer_reg
;
97 do_hw_poll_read_method
*reader
;
100 /* output port ID's */
113 static const struct hw_port_descriptor mn103ser_ports
[] = {
115 { "serial-0-receive", SERIAL0_RECEIVE
, 0, output_port
, },
116 { "serial-1-receive", SERIAL1_RECEIVE
, 0, output_port
, },
117 { "serial-2-receive", SERIAL2_RECEIVE
, 0, output_port
, },
118 { "serial-0-transmit", SERIAL0_SEND
, 0, output_port
, },
119 { "serial-1-transmit", SERIAL1_SEND
, 0, output_port
, },
120 { "serial-2-transmit", SERIAL2_SEND
, 0, output_port
, },
127 /* Finish off the partially created hw device. Attach our local
128 callbacks. Wire up our port names etc */
130 static hw_io_read_buffer_method mn103ser_io_read_buffer
;
131 static hw_io_write_buffer_method mn103ser_io_write_buffer
;
134 attach_mn103ser_regs (struct hw
*me
,
135 struct mn103ser
*serial
)
137 unsigned_word attach_address
;
139 unsigned attach_size
;
140 reg_property_spec reg
;
142 if (hw_find_property (me
, "reg") == NULL
)
143 hw_abort (me
, "Missing \"reg\" property");
145 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
146 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
147 hw_unit_address_to_attach_address (hw_parent (me
),
152 serial
->block
.base
= attach_address
;
153 hw_unit_size_to_attach_size (hw_parent (me
),
156 serial
->block
.bound
= attach_address
+ (attach_size
- 1);
157 hw_attach_address (hw_parent (me
),
159 attach_space
, attach_address
, attach_size
,
164 mn103ser_finish (struct hw
*me
)
166 struct mn103ser
*serial
;
169 serial
= HW_ZALLOC (me
, struct mn103ser
);
170 set_hw_data (me
, serial
);
171 set_hw_io_read_buffer (me
, mn103ser_io_read_buffer
);
172 set_hw_io_write_buffer (me
, mn103ser_io_write_buffer
);
173 set_hw_ports (me
, mn103ser_ports
);
175 /* Attach ourself to our parent bus */
176 attach_mn103ser_regs (me
, serial
);
178 /* If so configured, enable polled input */
179 if (hw_find_property (me
, "poll?") != NULL
180 && hw_find_boolean_property (me
, "poll?"))
182 serial
->reader
= sim_io_poll_read
;
186 serial
->reader
= sim_io_read
;
189 /* Initialize the serial device registers. */
190 for ( i
=0; i
<NR_SERIAL_DEVS
; ++i
)
192 serial
->device
[i
].txb
= 0;
193 serial
->device
[i
].rxb
= 0;
194 serial
->device
[i
].status
= 0;
195 serial
->device
[i
].control
= 0;
196 serial
->device
[i
].intmode
= 0;
197 serial
->device
[i
].event
= NULL
;
205 decode_addr (struct hw
*me
,
206 struct mn103ser
*serial
,
207 unsigned_word address
)
209 unsigned_word offset
;
210 offset
= address
- serial
->block
.base
;
213 case 0x00: return SC0CTR
;
214 case 0x04: return SC0ICR
;
215 case 0x08: return SC0TXB
;
216 case 0x09: return SC0RXB
;
217 case 0x0C: return SC0STR
;
218 case 0x10: return SC1CTR
;
219 case 0x14: return SC1ICR
;
220 case 0x18: return SC1TXB
;
221 case 0x19: return SC1RXB
;
222 case 0x1C: return SC1STR
;
223 case 0x20: return SC2CTR
;
224 case 0x24: return SC2ICR
;
225 case 0x28: return SC2TXB
;
226 case 0x29: return SC2RXB
;
227 case 0x2C: return SC2STR
;
228 case 0x2D: return SC2TIM
;
231 hw_abort (me
, "bad address");
238 do_polling_event (struct hw
*me
,
241 struct mn103ser
*serial
= hw_data(me
);
242 long serial_reg
= (long) data
;
249 rd
= dv_sockser_read (hw_system (me
));
257 count
= HW_IO_NOT_READY
;
262 count
= do_hw_poll_read (me
, serial
->reader
,
263 0/*STDIN*/, &c
, sizeof(c
));
269 case HW_IO_NOT_READY
:
271 serial
->device
[serial_reg
].rxb
= 0;
272 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
275 serial
->device
[serial_reg
].rxb
= c
;
276 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
277 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
280 /* Schedule next polling event */
281 serial
->device
[serial_reg
].event
282 = hw_event_queue_schedule (me
, 1000,
283 do_polling_event
, (void *)serial_reg
);
288 read_control_reg (struct hw
*me
,
289 struct mn103ser
*serial
,
290 unsigned_word serial_reg
,
294 /* really allow 1 byte read, too */
297 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].control
);
301 hw_abort (me
, "bad read size of %d bytes from SC%dCTR.", nr_bytes
,
308 read_intmode_reg (struct hw
*me
,
309 struct mn103ser
*serial
,
310 unsigned_word serial_reg
,
316 *(unsigned8
*)dest
= serial
->device
[serial_reg
].intmode
;
320 hw_abort (me
, "bad read size of %d bytes from SC%dICR.", nr_bytes
,
327 read_txb (struct hw
*me
,
328 struct mn103ser
*serial
,
329 unsigned_word serial_reg
,
335 *(unsigned8
*)dest
= serial
->device
[serial_reg
].txb
;
339 hw_abort (me
, "bad read size of %d bytes from SC%dTXB.", nr_bytes
,
346 read_rxb (struct hw
*me
,
347 struct mn103ser
*serial
,
348 unsigned_word serial_reg
,
354 *(unsigned8
*)dest
= serial
->device
[serial_reg
].rxb
;
355 /* Reception buffer is now empty. */
356 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
360 hw_abort (me
, "bad read size of %d bytes from SC%dRXB.", nr_bytes
,
367 read_status_reg (struct hw
*me
,
368 struct mn103ser
*serial
,
369 unsigned_word serial_reg
,
376 if ( (serial
->device
[serial_reg
].status
& SIO_STAT_RRDY
) == 0 )
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
;
389 rd
= dv_sockser_read (hw_system (me
));
397 count
= HW_IO_NOT_READY
;
402 count
= do_hw_poll_read (me
, serial
->reader
,
403 0/*STDIN*/, &c
, sizeof(c
));
408 case HW_IO_NOT_READY
:
410 serial
->device
[serial_reg
].rxb
= 0;
411 serial
->device
[serial_reg
].status
&= ~SIO_STAT_RRDY
;
414 serial
->device
[serial_reg
].rxb
= c
;
415 serial
->device
[serial_reg
].status
|= SIO_STAT_RRDY
;
416 hw_port_event (me
, serial_reg
+SERIAL0_RECEIVE
, 1);
419 /* schedule polling event */
420 serial
->device
[serial_reg
].event
421 = hw_event_queue_schedule (me
, 1000,
423 (void *) (long) serial_reg
);
428 *(unsigned8
*)dest
= (unsigned8
)serial
->device
[serial_reg
].status
;
430 else if ( nr_bytes
== 2 && serial_reg
!= SC2STR
)
432 *(unsigned16
*)dest
= H2LE_2 (serial
->device
[serial_reg
].status
);
436 hw_abort (me
, "bad read size of %d bytes from SC%dSTR.", nr_bytes
,
443 read_serial2_timer_reg (struct hw
*me
,
444 struct mn103ser
*serial
,
450 * (unsigned8
*) dest
= (unsigned8
) serial
->serial2_timer_reg
;
454 hw_abort (me
, "bad read size of %d bytes to SC2TIM.", nr_bytes
);
460 mn103ser_io_read_buffer (struct hw
*me
,
466 struct mn103ser
*serial
= hw_data (me
);
467 enum serial_register_types serial_reg
;
468 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
470 serial_reg
= decode_addr (me
, serial
, base
);
473 /* control registers */
477 read_control_reg(me
, serial
, serial_reg
-SC0CTR
, dest
, nr_bytes
);
478 HW_TRACE ((me
, "read - ctrl reg%d has 0x%x\n", serial_reg
-SC0CTR
,
479 *(unsigned8
*)dest
));
482 /* interrupt mode registers */
486 read_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, dest
, nr_bytes
);
487 HW_TRACE ((me
, "read - intmode reg%d has 0x%x\n", serial_reg
-SC0ICR
,
488 *(unsigned8
*)dest
));
491 /* transmission buffers */
495 read_txb(me
, serial
, serial_reg
-SC0TXB
, dest
, nr_bytes
);
496 HW_TRACE ((me
, "read - txb%d has %c\n", serial_reg
-SC0TXB
,
500 /* reception buffers */
504 read_rxb(me
, serial
, serial_reg
-SC0RXB
, dest
, nr_bytes
);
505 HW_TRACE ((me
, "read - rxb%d has %c\n", serial_reg
-SC0RXB
,
509 /* status registers */
513 read_status_reg(me
, serial
, serial_reg
-SC0STR
, dest
, nr_bytes
);
514 HW_TRACE ((me
, "read - status reg%d has 0x%x\n", serial_reg
-SC0STR
,
515 *(unsigned8
*)dest
));
519 read_serial2_timer_reg(me
, serial
, dest
, nr_bytes
);
520 HW_TRACE ((me
, "read - serial2 timer reg %d\n", *(unsigned8
*)dest
));
524 hw_abort(me
, "invalid address");
532 write_control_reg (struct hw
*me
,
533 struct mn103ser
*serial
,
534 unsigned_word serial_reg
,
538 unsigned16 val
= LE2H_2 (*(unsigned16
*)source
);
540 /* really allow 1 byte write, too */
543 if ( serial_reg
== 2 && (val
& 0x0C04) != 0 )
545 hw_abort(me
, "Cannot write to read-only bits of SC2CTR.");
549 serial
->device
[serial_reg
].control
= val
;
554 hw_abort (me
, "bad read size of %d bytes from SC%dSTR.", nr_bytes
,
561 write_intmode_reg (struct hw
*me
,
562 struct mn103ser
*serial
,
563 unsigned_word serial_reg
,
567 unsigned8 val
= *(unsigned8
*)source
;
571 /* Check for attempt to write to read-only bits of register. */
572 if ( ( serial_reg
== 2 && (val
& 0xCA) != 0 )
573 || ( serial_reg
!= 2 && (val
& 0x4A) != 0 ) )
575 hw_abort(me
, "Cannot write to read-only bits of SC%dICR.",
580 serial
->device
[serial_reg
].intmode
= val
;
585 hw_abort (me
, "bad write size of %d bytes to SC%dICR.", nr_bytes
,
592 write_txb (struct hw
*me
,
593 struct mn103ser
*serial
,
594 unsigned_word serial_reg
,
600 serial
->device
[serial_reg
].txb
= *(unsigned8
*)source
;
604 dv_sockser_write(hw_system (me
), * (char*) source
);
608 sim_io_write_stdout(hw_system (me
), (char *)source
, 1);
609 sim_io_flush_stdout(hw_system (me
));
612 hw_port_event (me
, serial_reg
+SERIAL0_SEND
, 1);
616 hw_abort (me
, "bad write size of %d bytes to SC%dTXB.", nr_bytes
,
623 write_serial2_timer_reg (struct hw
*me
,
624 struct mn103ser
*serial
,
630 serial
->serial2_timer_reg
= *(unsigned8
*)source
;
634 hw_abort (me
, "bad write size of %d bytes to SC2TIM.", nr_bytes
);
640 mn103ser_io_write_buffer (struct hw
*me
,
646 struct mn103ser
*serial
= hw_data (me
);
647 enum serial_register_types serial_reg
;
648 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
650 serial_reg
= decode_addr (me
, serial
, base
);
653 /* control registers */
657 HW_TRACE ((me
, "write - ctrl reg%d has 0x%x, nrbytes=%d.\n",
658 serial_reg
-SC0CTR
, *(unsigned8
*)source
, nr_bytes
));
659 write_control_reg(me
, serial
, serial_reg
-SC0CTR
, source
, nr_bytes
);
662 /* interrupt mode registers */
666 HW_TRACE ((me
, "write - intmode reg%d has 0x%x, nrbytes=%d.\n",
667 serial_reg
-SC0ICR
, *(unsigned8
*)source
, nr_bytes
));
668 write_intmode_reg(me
, serial
, serial_reg
-SC0ICR
, source
, nr_bytes
);
671 /* transmission buffers */
675 HW_TRACE ((me
, "write - txb%d has %c, nrbytes=%d.\n",
676 serial_reg
-SC0TXB
, *(char *)source
, nr_bytes
));
677 write_txb(me
, serial
, serial_reg
-SC0TXB
, source
, nr_bytes
);
680 /* reception buffers */
684 hw_abort(me
, "Cannot write to reception buffer.");
687 /* status registers */
691 hw_abort(me
, "Cannot write to status register.");
695 HW_TRACE ((me
, "read - serial2 timer reg %d (nrbytes=%d)\n",
696 *(unsigned8
*)source
, nr_bytes
));
697 write_serial2_timer_reg(me
, serial
, source
, nr_bytes
);
701 hw_abort(me
, "invalid address");
708 const struct hw_descriptor dv_mn103ser_descriptor
[] = {
709 { "mn103ser", mn103ser_finish
, },