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 <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include "sparc-semihost.h"
40 
41 typedef volatile uint8_t vuint8_t;
42 typedef volatile uint32_t vuint32_t;
43 
44 struct apbuart {
45     vuint32_t   data;
46     vuint32_t   status;
47     vuint32_t   control;
48 };
49 
50 #ifdef __GNUC__
51 #pragma GCC diagnostic ignored "-Wpragmas"
52 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
53 /* 'bsize' is used directly with malloc/realloc which confuses -fanalyzer */
54 #pragma GCC diagnostic ignored "-Warray-bounds"
55 #endif
56 
57 /* leon3 serial port */
58 #define uart    (*((struct apbuart *) 0x80000100))
59 
60 /* UART status register fields */
61 #define UART_DATA_READY           (1 <<  0)
62 #define UART_TRANSMIT_SHIFT_EMPTY (1 <<  1)
63 #define UART_TRANSMIT_FIFO_EMPTY  (1 <<  2)
64 #define UART_BREAK_RECEIVED       (1 <<  3)
65 #define UART_OVERRUN              (1 <<  4)
66 #define UART_PARITY_ERROR         (1 <<  5)
67 #define UART_FRAMING_ERROR        (1 <<  6)
68 #define UART_TRANSMIT_FIFO_HALF   (1 <<  7)
69 #define UART_RECEIVE_FIFO_HALF    (1 <<  8)
70 #define UART_TRANSMIT_FIFO_FULL   (1 <<  9)
71 #define UART_RECEIVE_FIFO_FULL    (1 << 10)
72 
73 /* UART control register fields */
74 #define UART_RECEIVE_ENABLE          (1 <<  0)
75 #define UART_TRANSMIT_ENABLE         (1 <<  1)
76 #define UART_RECEIVE_INTERRUPT       (1 <<  2)
77 #define UART_TRANSMIT_INTERRUPT      (1 <<  3)
78 #define UART_PARITY_SELECT           (1 <<  4)
79 #define UART_PARITY_ENABLE           (1 <<  5)
80 #define UART_FLOW_CONTROL            (1 <<  6)
81 #define UART_LOOPBACK                (1 <<  7)
82 #define UART_EXTERNAL_CLOCK          (1 <<  8)
83 #define UART_RECEIVE_FIFO_INTERRUPT  (1 <<  9)
84 #define UART_TRANSMIT_FIFO_INTERRUPT (1 << 10)
85 #define UART_FIFO_DEBUG_MODE         (1 << 11)
86 #define UART_OUTPUT_ENABLE           (1 << 12)
87 #define UART_FIFO_AVAILABLE          (1 << 31)
88 
89 int
sparc_putc(char c,FILE * file)90 sparc_putc(char c, FILE *file)
91 {
92 	(void) file;
93         uart.control |= UART_TRANSMIT_ENABLE;
94         while ((uart.status & UART_TRANSMIT_FIFO_FULL) != 0)
95                ;
96         uart.data = (uint8_t) c;
97 	return (unsigned char) c;
98 }
99 
100 #ifdef TINY_STDIO
101 
102 static int
sparc_getc(FILE * file)103 sparc_getc(FILE *file)
104 {
105 	(void) file;
106         uart.control |= UART_RECEIVE_ENABLE;
107         while ((uart.status & UART_DATA_READY) == 0)
108             ;
109         return (unsigned char) uart.data;
110 }
111 
112 static FILE __stdio = FDEV_SETUP_STREAM(sparc_putc, sparc_getc, NULL, _FDEV_SETUP_RW);
113 
114 #ifdef __strong_reference
115 #define STDIO_ALIAS(x) __strong_reference(stdin, x);
116 #else
117 #define STDIO_ALIAS(x) FILE *const x = &__stdio;
118 #endif
119 
120 FILE *const stdin = &__stdio;
121 STDIO_ALIAS(stdout);
122 STDIO_ALIAS(stderr);
123 
124 #endif
125