1 /* Lattice Mico32 UART model.
2 Contributed by Jon Beniston <jon@beniston.com>
4 Copyright (C) 2009-2024 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* This must come before any other includes. */
26 #include "sim-assert.h"
29 #include <sys/select.h>
34 unsigned base
; /* Base address of this UART. */
35 unsigned limit
; /* Limit address of this UART. */
45 struct hw_event
*event
;
50 #define LM32_UART_RBR 0x0
51 #define LM32_UART_THR 0x0
52 #define LM32_UART_IER 0x4
53 #define LM32_UART_IIR 0x8
54 #define LM32_UART_LCR 0xc
55 #define LM32_UART_MCR 0x10
56 #define LM32_UART_LSR 0x14
57 #define LM32_UART_MSR 0x18
58 #define LM32_UART_DIV 0x1c
60 #define LM32_UART_IER_RX_INT 0x1
61 #define LM32_UART_IER_TX_INT 0x2
63 #define MICOUART_IIR_TXRDY 0x2
64 #define MICOUART_IIR_RXRDY 0x4
66 #define LM32_UART_LSR_RX_RDY 0x01
67 #define LM32_UART_LSR_TX_RDY 0x20
69 #define LM32_UART_LCR_WLS_MASK 0x3
70 #define LM32_UART_LCR_WLS_5 0x0
71 #define LM32_UART_LCR_WLS_6 0x1
72 #define LM32_UART_LCR_WLS_7 0x2
73 #define LM32_UART_LCR_WLS_8 0x3
82 static const struct hw_port_descriptor lm32uart_ports
[] = {
83 {"int", INT_PORT
, 0, output_port
},
88 do_uart_tx_event (struct hw
*me
, void *data
)
90 struct lm32uart
*uart
= hw_data (me
);
93 /* Generate interrupt when transmission is complete. */
94 if (uart
->ier
& LM32_UART_IER_TX_INT
)
96 /* Generate interrupt */
97 hw_port_event (me
, INT_PORT
, 1);
100 /* Indicate which interrupt has occured. */
101 uart
->iir
= MICOUART_IIR_TXRDY
;
103 /* Indicate THR is empty. */
104 uart
->lsr
|= LM32_UART_LSR_TX_RDY
;
106 /* Output the character in the THR. */
107 c
= (char) uart
->thr
;
109 /* WLS field in LCR register specifies the number of bits to output. */
110 switch (uart
->lcr
& LM32_UART_LCR_WLS_MASK
)
112 case LM32_UART_LCR_WLS_5
:
115 case LM32_UART_LCR_WLS_6
:
118 case LM32_UART_LCR_WLS_7
:
126 lm32uart_io_write_buffer (struct hw
*me
,
128 int space
, unsigned_word base
, unsigned nr_bytes
)
130 struct lm32uart
*uart
= hw_data (me
);
132 const unsigned char *source_bytes
= source
;
135 HW_TRACE ((me
, "write to 0x%08lx length %d with 0x%x", (long) base
,
136 (int) nr_bytes
, value
));
139 value
= (source_bytes
[0] << 24)
140 | (source_bytes
[1] << 16) | (source_bytes
[2] << 8) | (source_bytes
[3]);
142 hw_abort (me
, "write of unsupported number of bytes: %d.", nr_bytes
);
144 uart_reg
= base
- uart
->base
;
149 /* Buffer the character to output. */
152 /* Indicate the THR is full. */
153 uart
->lsr
&= ~LM32_UART_LSR_TX_RDY
;
155 /* deassert interrupt when IER is loaded. */
156 uart
->iir
&= ~MICOUART_IIR_TXRDY
;
158 /* schedule an event to output the character. */
159 hw_event_queue_schedule (me
, 1, do_uart_tx_event
, 0);
164 if ((value
& LM32_UART_IER_TX_INT
)
165 && (uart
->lsr
& LM32_UART_LSR_TX_RDY
))
167 /* hw_event_queue_schedule (me, 1, do_uart_tx_event, 0); */
168 uart
->lsr
|= LM32_UART_LSR_TX_RDY
;
169 uart
->iir
|= MICOUART_IIR_TXRDY
;
170 hw_port_event (me
, INT_PORT
, 1);
172 else if ((value
& LM32_UART_IER_TX_INT
) == 0)
174 hw_port_event (me
, INT_PORT
, 0);
196 hw_abort (me
, "write to invalid register address: 0x%x.", uart_reg
);
203 lm32uart_io_read_buffer (struct hw
*me
,
205 int space
, unsigned_word base
, unsigned nr_bytes
)
207 struct lm32uart
*uart
= hw_data (me
);
210 unsigned char *dest_bytes
= dest
;
214 HW_TRACE ((me
, "read 0x%08lx length %d", (long) base
, (int) nr_bytes
));
216 uart_reg
= base
- uart
->base
;
222 uart
->lsr
&= ~LM32_UART_LSR_RX_RDY
;
237 /* Check to see if any data waiting in stdin. */
239 FD_SET (fileno (stdin
), &fd
);
242 if (select (fileno (stdin
) + 1, &fd
, NULL
, NULL
, &tv
))
243 uart
->lsr
|= LM32_UART_LSR_RX_RDY
;
253 hw_abort (me
, "read from invalid register address: 0x%x.", uart_reg
);
258 dest_bytes
[0] = value
>> 24;
259 dest_bytes
[1] = value
>> 16;
260 dest_bytes
[2] = value
>> 8;
261 dest_bytes
[3] = value
;
264 hw_abort (me
, "read of unsupported number of bytes: %d", nr_bytes
);
270 attach_lm32uart_regs (struct hw
*me
, struct lm32uart
*uart
)
272 unsigned_word attach_address
;
274 unsigned attach_size
;
275 reg_property_spec reg
;
277 if (hw_find_property (me
, "reg") == NULL
)
278 hw_abort (me
, "Missing \"reg\" property");
279 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
280 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
281 hw_unit_address_to_attach_address (hw_parent (me
),
283 &attach_space
, &attach_address
, me
);
284 uart
->base
= attach_address
;
285 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
286 uart
->limit
= attach_address
+ (attach_size
- 1);
287 hw_attach_address (hw_parent (me
),
288 0, attach_space
, attach_address
, attach_size
, me
);
292 lm32uart_finish (struct hw
*me
)
294 struct lm32uart
*uart
;
296 uart
= HW_ZALLOC (me
, struct lm32uart
);
297 set_hw_data (me
, uart
);
298 set_hw_io_read_buffer (me
, lm32uart_io_read_buffer
);
299 set_hw_io_write_buffer (me
, lm32uart_io_write_buffer
);
300 set_hw_ports (me
, lm32uart_ports
);
302 /* Attach ourself to our parent bus. */
303 attach_lm32uart_regs (me
, uart
);
305 /* Initialize the UART. */
312 uart
->lsr
= LM32_UART_LSR_TX_RDY
;
314 uart
->div
= 0; /* By setting to zero, characters are output immediately. */
317 const struct hw_descriptor dv_lm32uart_descriptor
[] = {
318 {"lm32uart", lm32uart_finish
,},