| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Device model for Cadence UART | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-11-07 10:00:24 +00:00
										 |  |  |  * Reference: Xilinx Zynq 7000 reference manual | 
					
						
							|  |  |  |  *   - http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
 | 
					
						
							|  |  |  |  *   - Chapter 19 UART Controller | 
					
						
							|  |  |  |  *   - Appendix B for Register details | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |  * Copyright (c) 2010 Xilinx Inc. | 
					
						
							|  |  |  |  * Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com) | 
					
						
							|  |  |  |  * Copyright (c) 2012 PetaLogix Pty Ltd. | 
					
						
							|  |  |  |  * Written by Haibing Ma | 
					
						
							|  |  |  |  *            M.Habib | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version | 
					
						
							|  |  |  |  * 2 of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License along | 
					
						
							|  |  |  |  * with this program; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:05 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-15 13:16:16 +01:00
										 |  |  | #include "hw/sysbus.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:45 +02:00
										 |  |  | #include "migration/vmstate.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-26 18:26:44 +04:00
										 |  |  | #include "chardev/char-fe.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-26 17:33:39 +04:00
										 |  |  | #include "chardev/char-serial.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-15 13:16:16 +01:00
										 |  |  | #include "qemu/timer.h"
 | 
					
						
							|  |  |  | #include "qemu/log.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:18 -07:00
										 |  |  | #include "hw/char/cadence_uart.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:42 +02:00
										 |  |  | #include "hw/irq.h"
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  | #include "hw/qdev-clock.h"
 | 
					
						
							|  |  |  | #include "trace.h"
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef CADENCE_UART_ERR_DEBUG
 | 
					
						
							|  |  |  | #define DB_PRINT(...) do { \
 | 
					
						
							|  |  |  |     fprintf(stderr,  ": %s: ", __func__); \ | 
					
						
							|  |  |  |     fprintf(stderr, ## __VA_ARGS__); \ | 
					
						
							| 
									
										
											  
											
												maint: Fix macros with broken 'do/while(0); ' usage
The point of writing a macro embedded in a 'do { ... } while (0)'
loop (particularly if the macro has multiple statements or would
otherwise end with an 'if' statement) is so that the macro can be
used as a drop-in statement with the caller supplying the
trailing ';'.  Although our coding style frowns on brace-less 'if':
  if (cond)
    statement;
  else
    something else;
that is the classic case where failure to use do/while(0) wrapping
would cause the 'else' to pair with any embedded 'if' in the macro
rather than the intended outer 'if'.  But conversely, if the macro
includes an embedded ';', then the same brace-less coding style
would now have two statements, making the 'else' a syntax error
rather than pairing with the outer 'if'.  Thus, even though our
coding style with required braces is not impacted, ending a macro
with ';' makes our code harder to port to projects that use
brace-less styles.
The change should have no semantic impact.  I was not able to
fully compile-test all of the changes (as some of them are
examples of the ugly bit-rotting debug print statements that are
completely elided by default, and I didn't want to recompile
with the necessary -D witnesses - cleaning those up is left as a
bite-sized task for another day); I did, however, audit that for
all files touched, all callers of the changed macros DID supply
a trailing ';' at the callsite, and did not appear to be used
as part of a brace-less conditional.
Found mechanically via: $ git grep -B1 'while (0);' | grep -A1 \\\\
Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <20171201232433.25193-7-eblake@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
											
										 
											2017-12-01 17:24:32 -06:00
										 |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     #define DB_PRINT(...)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UART_SR_INTR_RTRIG     0x00000001
 | 
					
						
							|  |  |  | #define UART_SR_INTR_REMPTY    0x00000002
 | 
					
						
							|  |  |  | #define UART_SR_INTR_RFUL      0x00000004
 | 
					
						
							|  |  |  | #define UART_SR_INTR_TEMPTY    0x00000008
 | 
					
						
							|  |  |  | #define UART_SR_INTR_TFUL      0x00000010
 | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  | /* somewhat awkwardly, TTRIG is misaligned between SR and ISR */ | 
					
						
							|  |  |  | #define UART_SR_TTRIG          0x00002000
 | 
					
						
							|  |  |  | #define UART_INTR_TTRIG        0x00000400
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | /* bits fields in CSR that correlate to CISR. If any of these bits are set in
 | 
					
						
							|  |  |  |  * SR, then the same bit in CISR is set high too */ | 
					
						
							|  |  |  | #define UART_SR_TO_CISR_MASK   0x0000001F
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UART_INTR_ROVR         0x00000020
 | 
					
						
							|  |  |  | #define UART_INTR_FRAME        0x00000040
 | 
					
						
							|  |  |  | #define UART_INTR_PARE         0x00000080
 | 
					
						
							|  |  |  | #define UART_INTR_TIMEOUT      0x00000100
 | 
					
						
							|  |  |  | #define UART_INTR_DMSI         0x00000200
 | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  | #define UART_INTR_TOVR         0x00001000
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define UART_SR_RACTIVE    0x00000400
 | 
					
						
							|  |  |  | #define UART_SR_TACTIVE    0x00000800
 | 
					
						
							|  |  |  | #define UART_SR_FDELT      0x00001000
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UART_CR_RXRST       0x00000001
 | 
					
						
							|  |  |  | #define UART_CR_TXRST       0x00000002
 | 
					
						
							|  |  |  | #define UART_CR_RX_EN       0x00000004
 | 
					
						
							|  |  |  | #define UART_CR_RX_DIS      0x00000008
 | 
					
						
							|  |  |  | #define UART_CR_TX_EN       0x00000010
 | 
					
						
							|  |  |  | #define UART_CR_TX_DIS      0x00000020
 | 
					
						
							|  |  |  | #define UART_CR_RST_TO      0x00000040
 | 
					
						
							|  |  |  | #define UART_CR_STARTBRK    0x00000080
 | 
					
						
							|  |  |  | #define UART_CR_STOPBRK     0x00000100
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UART_MR_CLKS            0x00000001
 | 
					
						
							|  |  |  | #define UART_MR_CHRL            0x00000006
 | 
					
						
							|  |  |  | #define UART_MR_CHRL_SH         1
 | 
					
						
							|  |  |  | #define UART_MR_PAR             0x00000038
 | 
					
						
							|  |  |  | #define UART_MR_PAR_SH          3
 | 
					
						
							|  |  |  | #define UART_MR_NBSTOP          0x000000C0
 | 
					
						
							|  |  |  | #define UART_MR_NBSTOP_SH       6
 | 
					
						
							|  |  |  | #define UART_MR_CHMODE          0x00000300
 | 
					
						
							|  |  |  | #define UART_MR_CHMODE_SH       8
 | 
					
						
							|  |  |  | #define UART_MR_UCLKEN          0x00000400
 | 
					
						
							|  |  |  | #define UART_MR_IRMODE          0x00000800
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UART_DATA_BITS_6       (0x3 << UART_MR_CHRL_SH)
 | 
					
						
							|  |  |  | #define UART_DATA_BITS_7       (0x2 << UART_MR_CHRL_SH)
 | 
					
						
							|  |  |  | #define UART_PARITY_ODD        (0x1 << UART_MR_PAR_SH)
 | 
					
						
							|  |  |  | #define UART_PARITY_EVEN       (0x0 << UART_MR_PAR_SH)
 | 
					
						
							|  |  |  | #define UART_STOP_BITS_1       (0x3 << UART_MR_NBSTOP_SH)
 | 
					
						
							|  |  |  | #define UART_STOP_BITS_2       (0x2 << UART_MR_NBSTOP_SH)
 | 
					
						
							|  |  |  | #define NORMAL_MODE            (0x0 << UART_MR_CHMODE_SH)
 | 
					
						
							|  |  |  | #define ECHO_MODE              (0x1 << UART_MR_CHMODE_SH)
 | 
					
						
							|  |  |  | #define LOCAL_LOOPBACK         (0x2 << UART_MR_CHMODE_SH)
 | 
					
						
							|  |  |  | #define REMOTE_LOOPBACK        (0x3 << UART_MR_CHMODE_SH)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  | #define UART_DEFAULT_REF_CLK (50 * 1000 * 1000)
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define R_CR       (0x00/4)
 | 
					
						
							|  |  |  | #define R_MR       (0x04/4)
 | 
					
						
							|  |  |  | #define R_IER      (0x08/4)
 | 
					
						
							|  |  |  | #define R_IDR      (0x0C/4)
 | 
					
						
							|  |  |  | #define R_IMR      (0x10/4)
 | 
					
						
							|  |  |  | #define R_CISR     (0x14/4)
 | 
					
						
							|  |  |  | #define R_BRGR     (0x18/4)
 | 
					
						
							|  |  |  | #define R_RTOR     (0x1C/4)
 | 
					
						
							|  |  |  | #define R_RTRIG    (0x20/4)
 | 
					
						
							|  |  |  | #define R_MCR      (0x24/4)
 | 
					
						
							|  |  |  | #define R_MSR      (0x28/4)
 | 
					
						
							|  |  |  | #define R_SR       (0x2C/4)
 | 
					
						
							|  |  |  | #define R_TX_RX    (0x30/4)
 | 
					
						
							|  |  |  | #define R_BDIV     (0x34/4)
 | 
					
						
							|  |  |  | #define R_FDEL     (0x38/4)
 | 
					
						
							|  |  |  | #define R_PMIN     (0x3C/4)
 | 
					
						
							|  |  |  | #define R_PWID     (0x40/4)
 | 
					
						
							|  |  |  | #define R_TTRIG    (0x44/4)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_update_status(CadenceUARTState *s) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:38 +00:00
										 |  |  |     s->r[R_SR] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     s->r[R_SR] |= s->rx_count == CADENCE_UART_RX_FIFO_SIZE ? UART_SR_INTR_RFUL | 
					
						
							|  |  |  |                                                            : 0; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:38 +00:00
										 |  |  |     s->r[R_SR] |= !s->rx_count ? UART_SR_INTR_REMPTY : 0; | 
					
						
							|  |  |  |     s->r[R_SR] |= s->rx_count >= s->r[R_RTRIG] ? UART_SR_INTR_RTRIG : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     s->r[R_SR] |= s->tx_count == CADENCE_UART_TX_FIFO_SIZE ? UART_SR_INTR_TFUL | 
					
						
							|  |  |  |                                                            : 0; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     s->r[R_SR] |= !s->tx_count ? UART_SR_INTR_TEMPTY : 0; | 
					
						
							|  |  |  |     s->r[R_SR] |= s->tx_count >= s->r[R_TTRIG] ? UART_SR_TTRIG : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     s->r[R_CISR] |= s->r[R_SR] & UART_SR_TO_CISR_MASK; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     s->r[R_CISR] |= s->r[R_SR] & UART_SR_TTRIG ? UART_INTR_TTRIG : 0; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     qemu_set_irq(s->irq, !!(s->r[R_IMR] & s->r[R_CISR])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void fifo_trigger_update(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 14:59:23 +00:00
										 |  |  |     if (s->r[R_RTOR]) { | 
					
						
							|  |  |  |         s->r[R_CISR] |= UART_INTR_TIMEOUT; | 
					
						
							|  |  |  |         uart_update_status(s); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_rx_reset(CadenceUARTState *s) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     s->rx_wpos = 0; | 
					
						
							|  |  |  |     s->rx_count = 0; | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:59 +03:00
										 |  |  |     qemu_chr_fe_accept_input(&s->chr); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_tx_reset(CadenceUARTState *s) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     s->tx_count = 0; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_send_breaks(CadenceUARTState *s) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     int break_enabled = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:59 +03:00
										 |  |  |     qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_BREAK, | 
					
						
							|  |  |  |                       &break_enabled); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_parameters_setup(CadenceUARTState *s) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     QEMUSerialSetParams ssp; | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     unsigned int baud_rate, packet_size, input_clk; | 
					
						
							|  |  |  |     input_clk = clock_get_hz(s->refclk); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     baud_rate = (s->r[R_MR] & UART_MR_CLKS) ? input_clk / 8 : input_clk; | 
					
						
							|  |  |  |     baud_rate /= (s->r[R_BRGR] * (s->r[R_BDIV] + 1)); | 
					
						
							|  |  |  |     trace_cadence_uart_baudrate(baud_rate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ssp.speed = baud_rate; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     packet_size = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (s->r[R_MR] & UART_MR_PAR) { | 
					
						
							|  |  |  |     case UART_PARITY_EVEN: | 
					
						
							|  |  |  |         ssp.parity = 'E'; | 
					
						
							|  |  |  |         packet_size++; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case UART_PARITY_ODD: | 
					
						
							|  |  |  |         ssp.parity = 'O'; | 
					
						
							|  |  |  |         packet_size++; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         ssp.parity = 'N'; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (s->r[R_MR] & UART_MR_CHRL) { | 
					
						
							|  |  |  |     case UART_DATA_BITS_6: | 
					
						
							|  |  |  |         ssp.data_bits = 6; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case UART_DATA_BITS_7: | 
					
						
							|  |  |  |         ssp.data_bits = 7; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         ssp.data_bits = 8; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (s->r[R_MR] & UART_MR_NBSTOP) { | 
					
						
							|  |  |  |     case UART_STOP_BITS_1: | 
					
						
							|  |  |  |         ssp.stop_bits = 1; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         ssp.stop_bits = 2; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     packet_size += ssp.data_bits + ssp.stop_bits; | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     if (ssp.speed == 0) { | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * Avoid division-by-zero below. | 
					
						
							|  |  |  |          * TODO: find something better | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         ssp.speed = 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-21 21:32:30 +05:30
										 |  |  |     s->char_tx_time = (NANOSECONDS_PER_SECOND / ssp.speed) * packet_size; | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:59 +03:00
										 |  |  |     qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int uart_can_receive(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							|  |  |  |     int ret = MAX(CADENCE_UART_RX_FIFO_SIZE, CADENCE_UART_TX_FIFO_SIZE); | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     uint32_t ch_mode = s->r[R_MR] & UART_MR_CHMODE; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     if (ch_mode == NORMAL_MODE || ch_mode == ECHO_MODE) { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |         ret = MIN(ret, CADENCE_UART_RX_FIFO_SIZE - s->rx_count); | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (ch_mode == REMOTE_LOOPBACK || ch_mode == ECHO_MODE) { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |         ret = MIN(ret, CADENCE_UART_TX_FIFO_SIZE - s->tx_count); | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_ctrl_update(CadenceUARTState *s) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     if (s->r[R_CR] & UART_CR_TXRST) { | 
					
						
							|  |  |  |         uart_tx_reset(s); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->r[R_CR] & UART_CR_RXRST) { | 
					
						
							|  |  |  |         uart_rx_reset(s); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s->r[R_CR] &= ~(UART_CR_TXRST | UART_CR_RXRST); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->r[R_CR] & UART_CR_STARTBRK && !(s->r[R_CR] & UART_CR_STOPBRK)) { | 
					
						
							|  |  |  |         uart_send_breaks(s); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     uint64_t new_rx_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((s->r[R_CR] & UART_CR_RX_DIS) || !(s->r[R_CR] & UART_CR_RX_EN)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     if (s->rx_count == CADENCE_UART_RX_FIFO_SIZE) { | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |         s->r[R_CISR] |= UART_INTR_ROVR; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         for (i = 0; i < size; i++) { | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:38 +00:00
										 |  |  |             s->rx_fifo[s->rx_wpos] = buf[i]; | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |             s->rx_wpos = (s->rx_wpos + 1) % CADENCE_UART_RX_FIFO_SIZE; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |             s->rx_count++; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |         timer_mod(s->fifo_trigger_handle, new_rx_time + | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |                                                 (s->char_tx_time * 4)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     uart_update_status(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  | static gboolean cadence_uart_xmit(GIOChannel *chan, GIOCondition cond, | 
					
						
							|  |  |  |                                   void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* instant drain the fifo when there's no back-end */ | 
					
						
							| 
									
										
										
										
											2017-07-06 15:08:52 +03:00
										 |  |  |     if (!qemu_chr_fe_backend_connected(&s->chr)) { | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  |         s->tx_count = 0; | 
					
						
							| 
									
										
										
										
											2014-07-15 17:18:44 +02:00
										 |  |  |         return FALSE; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!s->tx_count) { | 
					
						
							|  |  |  |         return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:55 +03:00
										 |  |  |     ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_count); | 
					
						
							| 
									
										
										
										
											2016-06-27 15:37:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ret >= 0) { | 
					
						
							|  |  |  |         s->tx_count -= ret; | 
					
						
							|  |  |  |         memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_count); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (s->tx_count) { | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:55 +03:00
										 |  |  |         guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP, | 
					
						
							| 
									
										
										
										
											2016-06-20 15:02:40 +02:00
										 |  |  |                                         cadence_uart_xmit, s); | 
					
						
							|  |  |  |         if (!r) { | 
					
						
							|  |  |  |             s->tx_count = 0; | 
					
						
							|  |  |  |             return FALSE; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uart_update_status(s); | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_write_tx_fifo(CadenceUARTState *s, const uint8_t *buf, | 
					
						
							|  |  |  |                                int size) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     if ((s->r[R_CR] & UART_CR_TX_DIS) || !(s->r[R_CR] & UART_CR_TX_EN)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     if (size > CADENCE_UART_TX_FIFO_SIZE - s->tx_count) { | 
					
						
							|  |  |  |         size = CADENCE_UART_TX_FIFO_SIZE - s->tx_count; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * This can only be a guest error via a bad tx fifo register push, | 
					
						
							|  |  |  |          * as can_receive() should stop remote loop and echo modes ever getting | 
					
						
							|  |  |  |          * us to here. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         qemu_log_mask(LOG_GUEST_ERROR, "cadence_uart: TxFIFO overflow"); | 
					
						
							|  |  |  |         s->r[R_CISR] |= UART_INTR_ROVR; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memcpy(s->tx_fifo + s->tx_count, buf, size); | 
					
						
							|  |  |  |     s->tx_count += size; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:40 +00:00
										 |  |  |     cadence_uart_xmit(NULL, G_IO_OUT, s); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void uart_receive(void *opaque, const uint8_t *buf, int size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     uint32_t ch_mode = s->r[R_MR] & UART_MR_CHMODE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     /* ignore characters when unclocked or in reset */ | 
					
						
							|  |  |  |     if (!clock_is_enabled(s->refclk) || device_is_in_reset(DEVICE(s))) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     if (ch_mode == NORMAL_MODE || ch_mode == ECHO_MODE) { | 
					
						
							|  |  |  |         uart_write_rx_fifo(opaque, buf, size); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (ch_mode == REMOTE_LOOPBACK || ch_mode == ECHO_MODE) { | 
					
						
							|  |  |  |         uart_write_tx_fifo(s, buf, size); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												chardev: Use QEMUChrEvent enum in IOEventHandler typedef
The Chardev events are listed in the QEMUChrEvent enum.
By using the enum in the IOEventHandler typedef we:
- make the IOEventHandler type more explicit (this handler
  process out-of-band information, while the IOReadHandler
  is in-band),
- help static code analyzers.
This patch was produced with the following spatch script:
  @match@
  expression backend, opaque, context, set_open;
  identifier fd_can_read, fd_read, fd_event, be_change;
  @@
  qemu_chr_fe_set_handlers(backend, fd_can_read, fd_read, fd_event,
                           be_change, opaque, context, set_open);
  @depends on match@
  identifier opaque, event;
  identifier match.fd_event;
  @@
   static
  -void fd_event(void *opaque, int event)
  +void fd_event(void *opaque, QEMUChrEvent event)
   {
   ...
   }
Then the typedef was modified manually in
include/chardev/char-fe.h.
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Acked-by: Corey Minyard <cminyard@mvista.com>
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20191218172009.8868-15-philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
											
										 
											2019-12-18 18:20:09 +01:00
										 |  |  | static void uart_event(void *opaque, QEMUChrEvent event) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     uint8_t buf = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     /* ignore characters when unclocked or in reset */ | 
					
						
							|  |  |  |     if (!clock_is_enabled(s->refclk) || device_is_in_reset(DEVICE(s))) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     if (event == CHR_EVENT_BREAK) { | 
					
						
							|  |  |  |         uart_write_rx_fifo(opaque, &buf, 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uart_update_status(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  | static void uart_read_rx_fifo(CadenceUARTState *s, uint32_t *c) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     if ((s->r[R_CR] & UART_CR_RX_DIS) || !(s->r[R_CR] & UART_CR_RX_EN)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->rx_count) { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |         uint32_t rx_rpos = (CADENCE_UART_RX_FIFO_SIZE + s->rx_wpos - | 
					
						
							|  |  |  |                             s->rx_count) % CADENCE_UART_RX_FIFO_SIZE; | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:38 +00:00
										 |  |  |         *c = s->rx_fifo[rx_rpos]; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |         s->rx_count--; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:59 +03:00
										 |  |  |         qemu_chr_fe_accept_input(&s->chr); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         *c = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uart_update_status(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-23 12:30:10 +02:00
										 |  |  | static void uart_write(void *opaque, hwaddr offset, | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |                           uint64_t value, unsigned size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 19:08:04 +10:00
										 |  |  |     DB_PRINT(" offset:%x data:%08x\n", (unsigned)offset, (unsigned)value); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     offset >>= 2; | 
					
						
							| 
									
										
										
										
											2016-04-18 13:07:35 +03:00
										 |  |  |     if (offset >= CADENCE_UART_R_MAX) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     switch (offset) { | 
					
						
							|  |  |  |     case R_IER: /* ier (wts imr) */ | 
					
						
							|  |  |  |         s->r[R_IMR] |= value; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case R_IDR: /* idr (wtc imr) */ | 
					
						
							|  |  |  |         s->r[R_IMR] &= ~value; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case R_IMR: /* imr (read only) */ | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case R_CISR: /* cisr (wtc) */ | 
					
						
							|  |  |  |         s->r[R_CISR] &= ~value; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case R_TX_RX: /* UARTDR */ | 
					
						
							|  |  |  |         switch (s->r[R_MR] & UART_MR_CHMODE) { | 
					
						
							|  |  |  |         case NORMAL_MODE: | 
					
						
							|  |  |  |             uart_write_tx_fifo(s, (uint8_t *) &value, 1); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case LOCAL_LOOPBACK: | 
					
						
							|  |  |  |             uart_write_rx_fifo(opaque, (uint8_t *) &value, 1); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2016-11-07 10:00:24 +00:00
										 |  |  |     case R_BRGR: /* Baud rate generator */ | 
					
						
							|  |  |  |         if (value >= 0x01) { | 
					
						
							|  |  |  |             s->r[offset] = value & 0xFFFF; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case R_BDIV:    /* Baud rate divider */ | 
					
						
							|  |  |  |         if (value >= 0x04) { | 
					
						
							|  |  |  |             s->r[offset] = value & 0xFF; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     default: | 
					
						
							|  |  |  |         s->r[offset] = value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (offset) { | 
					
						
							|  |  |  |     case R_CR: | 
					
						
							|  |  |  |         uart_ctrl_update(s); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case R_MR: | 
					
						
							|  |  |  |         uart_parameters_setup(s); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:37 +00:00
										 |  |  |     uart_update_status(s); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-23 12:30:10 +02:00
										 |  |  | static uint64_t uart_read(void *opaque, hwaddr offset, | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |         unsigned size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     uint32_t c = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     offset >>= 2; | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     if (offset >= CADENCE_UART_R_MAX) { | 
					
						
							| 
									
										
										
										
											2012-10-19 19:08:04 +10:00
										 |  |  |         c = 0; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     } else if (offset == R_TX_RX) { | 
					
						
							|  |  |  |         uart_read_rx_fifo(s, &c); | 
					
						
							| 
									
										
										
										
											2012-10-19 19:08:04 +10:00
										 |  |  |     } else { | 
					
						
							|  |  |  |        c = s->r[offset]; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-10-19 19:08:04 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     DB_PRINT(" offset:%x data:%08x\n", (unsigned)(offset << 2), (unsigned)c); | 
					
						
							|  |  |  |     return c; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const MemoryRegionOps uart_ops = { | 
					
						
							|  |  |  |     .read = uart_read, | 
					
						
							|  |  |  |     .write = uart_write, | 
					
						
							|  |  |  |     .endianness = DEVICE_NATIVE_ENDIAN, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  | static void cadence_uart_reset_init(Object *obj, ResetType type) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     CadenceUARTState *s = CADENCE_UART(obj); | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     s->r[R_CR] = 0x00000128; | 
					
						
							|  |  |  |     s->r[R_IMR] = 0; | 
					
						
							|  |  |  |     s->r[R_CISR] = 0; | 
					
						
							|  |  |  |     s->r[R_RTRIG] = 0x00000020; | 
					
						
							| 
									
										
										
										
											2016-10-28 14:12:31 +01:00
										 |  |  |     s->r[R_BRGR] = 0x0000028B; | 
					
						
							|  |  |  |     s->r[R_BDIV] = 0x0000000F; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     s->r[R_TTRIG] = 0x00000020; | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void cadence_uart_reset_hold(Object *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CadenceUARTState *s = CADENCE_UART(obj); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     uart_rx_reset(s); | 
					
						
							|  |  |  |     uart_tx_reset(s); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:38 +00:00
										 |  |  |     uart_update_status(s); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  | static void cadence_uart_realize(DeviceState *dev, Error **errp) | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = CADENCE_UART(dev); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL, | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  |                                           fifo_trigger_update, s); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 12:52:59 +03:00
										 |  |  |     qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive, | 
					
						
							| 
									
										
										
										
											2017-07-06 15:08:49 +03:00
										 |  |  |                              uart_event, NULL, s, NULL, true); | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  | static void cadence_uart_refclk_update(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CadenceUARTState *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* recompute uart's speed on clock change */ | 
					
						
							|  |  |  |     uart_parameters_setup(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  | static void cadence_uart_init(Object *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SysBusDevice *sbd = SYS_BUS_DEVICE(obj); | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = CADENCE_UART(obj); | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     memory_region_init_io(&s->iomem, obj, &uart_ops, s, "uart", 0x1000); | 
					
						
							|  |  |  |     sysbus_init_mmio(sbd, &s->iomem); | 
					
						
							|  |  |  |     sysbus_init_irq(sbd, &s->irq); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     s->refclk = qdev_init_clock_in(DEVICE(obj), "refclk", | 
					
						
							|  |  |  |             cadence_uart_refclk_update, s); | 
					
						
							|  |  |  |     /* initialize the frequency in case the clock remains unconnected */ | 
					
						
							|  |  |  |     clock_set_hz(s->refclk, UART_DEFAULT_REF_CLK); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:32:30 +05:30
										 |  |  |     s->char_tx_time = (NANOSECONDS_PER_SECOND / 9600) * 10; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  | static int cadence_uart_pre_load(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CadenceUARTState *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* the frequency will be overriden if the refclk field is present */ | 
					
						
							|  |  |  |     clock_set_hz(s->refclk, UART_DEFAULT_REF_CLK); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | static int cadence_uart_post_load(void *opaque, int version_id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     CadenceUARTState *s = opaque; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 14:59:22 +00:00
										 |  |  |     /* Ensure these two aren't invalid numbers */ | 
					
						
							|  |  |  |     if (s->r[R_BRGR] < 1 || s->r[R_BRGR] & ~0xFFFF || | 
					
						
							|  |  |  |         s->r[R_BDIV] <= 3 || s->r[R_BDIV] & ~0xFF) { | 
					
						
							|  |  |  |         /* Value is invalid, abort */ | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     uart_parameters_setup(s); | 
					
						
							|  |  |  |     uart_update_status(s); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const VMStateDescription vmstate_cadence_uart = { | 
					
						
							|  |  |  |     .name = "cadence_uart", | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     .version_id = 3, | 
					
						
							| 
									
										
										
										
											2014-01-06 10:16:39 +00:00
										 |  |  |     .minimum_version_id = 2, | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     .pre_load = cadence_uart_pre_load, | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     .post_load = cadence_uart_post_load, | 
					
						
							|  |  |  |     .fields = (VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |         VMSTATE_UINT32_ARRAY(r, CadenceUARTState, CADENCE_UART_R_MAX), | 
					
						
							|  |  |  |         VMSTATE_UINT8_ARRAY(rx_fifo, CadenceUARTState, | 
					
						
							|  |  |  |                             CADENCE_UART_RX_FIFO_SIZE), | 
					
						
							|  |  |  |         VMSTATE_UINT8_ARRAY(tx_fifo, CadenceUARTState, | 
					
						
							|  |  |  |                             CADENCE_UART_TX_FIFO_SIZE), | 
					
						
							|  |  |  |         VMSTATE_UINT32(rx_count, CadenceUARTState), | 
					
						
							|  |  |  |         VMSTATE_UINT32(tx_count, CadenceUARTState), | 
					
						
							|  |  |  |         VMSTATE_UINT32(rx_wpos, CadenceUARTState), | 
					
						
							|  |  |  |         VMSTATE_TIMER_PTR(fifo_trigger_handle, CadenceUARTState), | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |         VMSTATE_CLOCK_V(refclk, CadenceUARTState, 3), | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 16:59:31 +01:00
										 |  |  | static Property cadence_uart_properties[] = { | 
					
						
							|  |  |  |     DEFINE_PROP_CHR("chardev", CadenceUARTState, chr), | 
					
						
							|  |  |  |     DEFINE_PROP_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | static void cadence_uart_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     ResettableClass *rc = RESETTABLE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  |     dc->realize = cadence_uart_realize; | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     dc->vmsd = &vmstate_cadence_uart; | 
					
						
							| 
									
										
										
										
											2020-04-06 15:52:49 +02:00
										 |  |  |     rc->phases.enter = cadence_uart_reset_init; | 
					
						
							|  |  |  |     rc->phases.hold  = cadence_uart_reset_hold; | 
					
						
							| 
									
										
										
										
											2020-01-10 19:30:32 +04:00
										 |  |  |     device_class_set_props(dc, cadence_uart_properties); | 
					
						
							| 
									
										
										
										
											2016-06-06 16:59:31 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-10 16:19:07 +01:00
										 |  |  | static const TypeInfo cadence_uart_info = { | 
					
						
							| 
									
										
										
										
											2013-07-24 21:23:29 +02:00
										 |  |  |     .name          = TYPE_CADENCE_UART, | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     .parent        = TYPE_SYS_BUS_DEVICE, | 
					
						
							| 
									
										
										
										
											2015-05-14 19:23:15 -07:00
										 |  |  |     .instance_size = sizeof(CadenceUARTState), | 
					
						
							| 
									
										
										
										
											2015-02-27 11:48:58 +10:00
										 |  |  |     .instance_init = cadence_uart_init, | 
					
						
							| 
									
										
										
										
											2012-03-05 14:39:10 +10:00
										 |  |  |     .class_init    = cadence_uart_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void cadence_uart_register_types(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     type_register_static(&cadence_uart_info); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type_init(cadence_uart_register_types) |