1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2023 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "sparc-semihost.h"
37 
38 typedef volatile uint8_t vuint8_t;
39 typedef volatile uint32_t vuint32_t;
40 
41 struct apbuart {
42     vuint32_t   data;
43     vuint32_t   status;
44     vuint32_t   control;
45 };
46 
47 #ifdef __GNUC__
48 #pragma GCC diagnostic ignored "-Wpragmas"
49 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
50 /* 'bsize' is used directly with malloc/realloc which confuses -fanalyzer */
51 #pragma GCC diagnostic ignored "-Warray-bounds"
52 #endif
53 
54 /* leon3 serial port */
55 #define uart    (*((struct apbuart *) 0x80000100))
56 
57 /* UART status register fields */
58 #define UART_DATA_READY           (1 <<  0)
59 #define UART_TRANSMIT_SHIFT_EMPTY (1 <<  1)
60 #define UART_TRANSMIT_FIFO_EMPTY  (1 <<  2)
61 #define UART_BREAK_RECEIVED       (1 <<  3)
62 #define UART_OVERRUN              (1 <<  4)
63 #define UART_PARITY_ERROR         (1 <<  5)
64 #define UART_FRAMING_ERROR        (1 <<  6)
65 #define UART_TRANSMIT_FIFO_HALF   (1 <<  7)
66 #define UART_RECEIVE_FIFO_HALF    (1 <<  8)
67 #define UART_TRANSMIT_FIFO_FULL   (1 <<  9)
68 #define UART_RECEIVE_FIFO_FULL    (1 << 10)
69 
70 /* UART control register fields */
71 #define UART_RECEIVE_ENABLE          (1 <<  0)
72 #define UART_TRANSMIT_ENABLE         (1 <<  1)
73 #define UART_RECEIVE_INTERRUPT       (1 <<  2)
74 #define UART_TRANSMIT_INTERRUPT      (1 <<  3)
75 #define UART_PARITY_SELECT           (1 <<  4)
76 #define UART_PARITY_ENABLE           (1 <<  5)
77 #define UART_FLOW_CONTROL            (1 <<  6)
78 #define UART_LOOPBACK                (1 <<  7)
79 #define UART_EXTERNAL_CLOCK          (1 <<  8)
80 #define UART_RECEIVE_FIFO_INTERRUPT  (1 <<  9)
81 #define UART_TRANSMIT_FIFO_INTERRUPT (1 << 10)
82 #define UART_FIFO_DEBUG_MODE         (1 << 11)
83 #define UART_OUTPUT_ENABLE           (1 << 12)
84 #define UART_FIFO_AVAILABLE          (1 << 31)
85 
86 int
sparc_putc(char c,FILE * file)87 sparc_putc(char c, FILE *file)
88 {
89 	(void) file;
90         uart.control |= UART_TRANSMIT_ENABLE;
91         while ((uart.status & UART_TRANSMIT_FIFO_FULL) != 0)
92                ;
93         uart.data = (uint8_t) c;
94 	return (unsigned char) c;
95 }
96 
97 #ifdef TINY_STDIO
98 
99 static int
sparc_getc(FILE * file)100 sparc_getc(FILE *file)
101 {
102 	(void) file;
103         uart.control |= UART_RECEIVE_ENABLE;
104         while ((uart.status & UART_DATA_READY) == 0)
105             ;
106         return (unsigned char) uart.data;
107 }
108 
109 static FILE __stdio = FDEV_SETUP_STREAM(sparc_putc, sparc_getc, NULL, _FDEV_SETUP_RW);
110 
111 #ifdef __strong_reference
112 #define STDIO_ALIAS(x) __strong_reference(stdin, x);
113 #else
114 #define STDIO_ALIAS(x) FILE *const x = &__stdio;
115 #endif
116 
117 FILE *const stdin = &__stdio;
118 STDIO_ALIAS(stdout);
119 STDIO_ALIAS(stderr);
120 
121 #endif
122