1 /* This file is part of the program GDB, the GNU debugger.
3 Copyright (C) 1998-2022 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/>.
21 /* This must come before any other includes. */
26 #include "dv-sockser.h"
32 mn103ser - mn103002 serial devices 0, 1 and 2.
37 Implements the mn103002 serial interfaces as described in the
43 reg = <serial-addr> <serial-size>
51 /* The serial devices' registers' address block */
53 struct mn103ser_block {
60 enum serial_register_types {
80 #define NR_SERIAL_DEVS 3
81 #define SIO_STAT_RRDY 0x0010
83 typedef struct _mn10300_serial {
84 uint16_t status, control;
85 uint8_t txb, rxb, intmode;
86 struct hw_event *event;
92 struct mn103ser_block block;
93 mn10300_serial device[NR_SERIAL_DEVS];
94 uint8_t serial2_timer_reg;
95 do_hw_poll_read_method *reader;
98 /* output port ID's */
111 static const struct hw_port_descriptor mn103ser_ports[] = {
113 { "serial-0-receive", SERIAL0_RECEIVE, 0, output_port, },
114 { "serial-1-receive", SERIAL1_RECEIVE, 0, output_port, },
115 { "serial-2-receive", SERIAL2_RECEIVE, 0, output_port, },
116 { "serial-0-transmit", SERIAL0_SEND, 0, output_port, },
117 { "serial-1-transmit", SERIAL1_SEND, 0, output_port, },
118 { "serial-2-transmit", SERIAL2_SEND, 0, output_port, },
125 /* Finish off the partially created hw device. Attach our local
126 callbacks. Wire up our port names etc */
128 static hw_io_read_buffer_method mn103ser_io_read_buffer;
129 static hw_io_write_buffer_method mn103ser_io_write_buffer;
132 attach_mn103ser_regs (struct hw *me,
133 struct mn103ser *serial)
135 unsigned_word attach_address;
137 unsigned attach_size;
138 reg_property_spec reg;
140 if (hw_find_property (me, "reg") == NULL)
141 hw_abort (me, "Missing \"reg\" property");
143 if (!hw_find_reg_array_property (me, "reg", 0, ®))
144 hw_abort (me, "\"reg\" property must contain three addr/size entries");
145 hw_unit_address_to_attach_address (hw_parent (me),
150 serial->block.base = attach_address;
151 hw_unit_size_to_attach_size (hw_parent (me),
154 serial->block.bound = attach_address + (attach_size - 1);
155 hw_attach_address (hw_parent (me),
157 attach_space, attach_address, attach_size,
162 mn103ser_finish (struct hw *me)
164 struct mn103ser *serial;
167 serial = HW_ZALLOC (me, struct mn103ser);
168 set_hw_data (me, serial);
169 set_hw_io_read_buffer (me, mn103ser_io_read_buffer);
170 set_hw_io_write_buffer (me, mn103ser_io_write_buffer);
171 set_hw_ports (me, mn103ser_ports);
173 /* Attach ourself to our parent bus */
174 attach_mn103ser_regs (me, serial);
176 /* If so configured, enable polled input */
177 if (hw_find_property (me, "poll?") != NULL
178 && hw_find_boolean_property (me, "poll?"))
180 serial->reader = sim_io_poll_read;
184 serial->reader = sim_io_read;
187 /* Initialize the serial device registers. */
188 for ( i=0; i<NR_SERIAL_DEVS; ++i )
190 serial->device[i].txb = 0;
191 serial->device[i].rxb = 0;
192 serial->device[i].status = 0;
193 serial->device[i].control = 0;
194 serial->device[i].intmode = 0;
195 serial->device[i].event = NULL;
203 decode_addr (struct hw *me,
204 struct mn103ser *serial,
205 unsigned_word address)
207 unsigned_word offset;
208 offset = address - serial->block.base;
211 case 0x00: return SC0CTR;
212 case 0x04: return SC0ICR;
213 case 0x08: return SC0TXB;
214 case 0x09: return SC0RXB;
215 case 0x0C: return SC0STR;
216 case 0x10: return SC1CTR;
217 case 0x14: return SC1ICR;
218 case 0x18: return SC1TXB;
219 case 0x19: return SC1RXB;
220 case 0x1C: return SC1STR;
221 case 0x20: return SC2CTR;
222 case 0x24: return SC2ICR;
223 case 0x28: return SC2TXB;
224 case 0x29: return SC2RXB;
225 case 0x2C: return SC2STR;
226 case 0x2D: return SC2TIM;
229 hw_abort (me, "bad address");
236 do_polling_event (struct hw *me,
239 SIM_DESC sd = hw_system (me);
240 struct mn103ser *serial = hw_data(me);
241 long serial_reg = (uintptr_t) data;
245 status = dv_sockser_status (sd);
246 if (!(status & DV_SOCKSER_DISCONNECTED))
249 rd = dv_sockser_read (sd);
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 *)(uintptr_t)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 *(uint16_t *)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 *(uint8_t *)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 *(uint8_t *)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 *(uint8_t *)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 )
378 SIM_DESC sd = hw_system (me);
382 /* Kill current poll event */
383 if ( NULL != serial->device[serial_reg].event )
385 hw_event_queue_deschedule (me, serial->device[serial_reg].event);
386 serial->device[serial_reg].event = NULL;
389 status = dv_sockser_status (sd);
390 if (!(status & DV_SOCKSER_DISCONNECTED))
393 rd = dv_sockser_read (sd);
401 count = HW_IO_NOT_READY;
406 count = do_hw_poll_read (me, serial->reader,
407 0/*STDIN*/, &c, sizeof(c));
412 case HW_IO_NOT_READY:
414 serial->device[serial_reg].rxb = 0;
415 serial->device[serial_reg].status &= ~SIO_STAT_RRDY;
418 serial->device[serial_reg].rxb = c;
419 serial->device[serial_reg].status |= SIO_STAT_RRDY;
420 hw_port_event (me, serial_reg+SERIAL0_RECEIVE, 1);
423 /* schedule polling event */
424 serial->device[serial_reg].event
425 = hw_event_queue_schedule (me, 1000,
427 (void *)(uintptr_t)serial_reg);
432 *(uint8_t *)dest = (uint8_t)serial->device[serial_reg].status;
434 else if ( nr_bytes == 2 && serial_reg != SC2STR )
436 *(uint16_t *)dest = H2LE_2 (serial->device[serial_reg].status);
440 hw_abort (me, "bad read size of %d bytes from SC%dSTR.", nr_bytes,
447 read_serial2_timer_reg (struct hw *me,
448 struct mn103ser *serial,
454 * (uint8_t *) dest = (uint8_t) serial->serial2_timer_reg;
458 hw_abort (me, "bad read size of %d bytes to SC2TIM.", nr_bytes);
464 mn103ser_io_read_buffer (struct hw *me,
470 struct mn103ser *serial = hw_data (me);
471 enum serial_register_types serial_reg;
472 HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
474 serial_reg = decode_addr (me, serial, base);
477 /* control registers */
481 read_control_reg(me, serial, serial_reg-SC0CTR, dest, nr_bytes);
482 HW_TRACE ((me, "read - ctrl reg%d has 0x%x\n", serial_reg-SC0CTR,
486 /* interrupt mode registers */
490 read_intmode_reg(me, serial, serial_reg-SC0ICR, dest, nr_bytes);
491 HW_TRACE ((me, "read - intmode reg%d has 0x%x\n", serial_reg-SC0ICR,
495 /* transmission buffers */
499 read_txb(me, serial, serial_reg-SC0TXB, dest, nr_bytes);
500 HW_TRACE ((me, "read - txb%d has %c\n", serial_reg-SC0TXB,
504 /* reception buffers */
508 read_rxb(me, serial, serial_reg-SC0RXB, dest, nr_bytes);
509 HW_TRACE ((me, "read - rxb%d has %c\n", serial_reg-SC0RXB,
513 /* status registers */
517 read_status_reg(me, serial, serial_reg-SC0STR, dest, nr_bytes);
518 HW_TRACE ((me, "read - status reg%d has 0x%x\n", serial_reg-SC0STR,
523 read_serial2_timer_reg(me, serial, dest, nr_bytes);
524 HW_TRACE ((me, "read - serial2 timer reg %d\n", *(uint8_t *)dest));
528 hw_abort(me, "invalid address");
536 write_control_reg (struct hw *me,
537 struct mn103ser *serial,
538 unsigned_word serial_reg,
542 uint16_t val = LE2H_2 (*(uint16_t *)source);
544 /* really allow 1 byte write, too */
547 if ( serial_reg == 2 && (val & 0x0C04) != 0 )
549 hw_abort(me, "Cannot write to read-only bits of SC2CTR.");
553 serial->device[serial_reg].control = val;
558 hw_abort (me, "bad read size of %d bytes from SC%dSTR.", nr_bytes,
565 write_intmode_reg (struct hw *me,
566 struct mn103ser *serial,
567 unsigned_word serial_reg,
571 uint8_t val = *(uint8_t *)source;
575 /* Check for attempt to write to read-only bits of register. */
576 if ( ( serial_reg == 2 && (val & 0xCA) != 0 )
577 || ( serial_reg != 2 && (val & 0x4A) != 0 ) )
579 hw_abort(me, "Cannot write to read-only bits of SC%dICR.",
584 serial->device[serial_reg].intmode = val;
589 hw_abort (me, "bad write size of %d bytes to SC%dICR.", nr_bytes,
596 write_txb (struct hw *me,
597 struct mn103ser *serial,
598 unsigned_word serial_reg,
604 SIM_DESC sd = hw_system (me);
607 serial->device[serial_reg].txb = *(uint8_t *)source;
609 status = dv_sockser_status (sd);
610 if (!(status & DV_SOCKSER_DISCONNECTED))
612 dv_sockser_write(sd, * (char*) source);
616 sim_io_write_stdout(sd, (char *)source, 1);
617 sim_io_flush_stdout(sd);
620 hw_port_event (me, serial_reg+SERIAL0_SEND, 1);
624 hw_abort (me, "bad write size of %d bytes to SC%dTXB.", nr_bytes,
631 write_serial2_timer_reg (struct hw *me,
632 struct mn103ser *serial,
638 serial->serial2_timer_reg = *(uint8_t *)source;
642 hw_abort (me, "bad write size of %d bytes to SC2TIM.", nr_bytes);
648 mn103ser_io_write_buffer (struct hw *me,
654 struct mn103ser *serial = hw_data (me);
655 enum serial_register_types serial_reg;
656 HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
658 serial_reg = decode_addr (me, serial, base);
661 /* control registers */
665 HW_TRACE ((me, "write - ctrl reg%d has 0x%x, nrbytes=%d.\n",
666 serial_reg-SC0CTR, *(uint8_t *)source, nr_bytes));
667 write_control_reg(me, serial, serial_reg-SC0CTR, source, nr_bytes);
670 /* interrupt mode registers */
674 HW_TRACE ((me, "write - intmode reg%d has 0x%x, nrbytes=%d.\n",
675 serial_reg-SC0ICR, *(uint8_t *)source, nr_bytes));
676 write_intmode_reg(me, serial, serial_reg-SC0ICR, source, nr_bytes);
679 /* transmission buffers */
683 HW_TRACE ((me, "write - txb%d has %c, nrbytes=%d.\n",
684 serial_reg-SC0TXB, *(char *)source, nr_bytes));
685 write_txb(me, serial, serial_reg-SC0TXB, source, nr_bytes);
688 /* reception buffers */
692 hw_abort(me, "Cannot write to reception buffer.");
695 /* status registers */
699 hw_abort(me, "Cannot write to status register.");
703 HW_TRACE ((me, "read - serial2 timer reg %d (nrbytes=%d)\n",
704 *(uint8_t *)source, nr_bytes));
705 write_serial2_timer_reg(me, serial, source, nr_bytes);
709 hw_abort(me, "invalid address");
716 const struct hw_descriptor dv_mn103ser_descriptor[] = {
717 { "mn103ser", mn103ser_finish, },