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 "mips-semihost.h"
37
38 #define UART0 0x3f8
39
40 typedef volatile uint8_t vuint8_t;
41
42 #define REG_SPACING 8
43
44 #if REG_SPACING
45 #define __cat(a,b) a ## b
46 #define reg_pad(n) uint8_t __cat(pad, n)[REG_SPACING-1];
47 #else
48 #define reg_pad(n)
49 #endif
50
51
52 struct uart_8250 {
53 vuint8_t data;
54 reg_pad(0)
55 vuint8_t ier;
56 reg_pad(1)
57 vuint8_t iir;
58 reg_pad(2)
59 vuint8_t lcr;
60 reg_pad(3)
61
62 vuint8_t mcr;
63 reg_pad(4)
64 vuint8_t lsr;
65 reg_pad(5)
66 vuint8_t msr;
67 reg_pad(6)
68 vuint8_t scr;
69 reg_pad(7)
70 };
71
72 #ifdef __GNUC__
73 #pragma GCC diagnostic ignored "-Wpragmas"
74 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
75 /* 'bsize' is used directly with malloc/realloc which confuses -fanalyzer */
76 #pragma GCC diagnostic ignored "-Warray-bounds"
77 #endif
78
79 /* malta serial port */
80 #define uart (*((struct uart_8250 *) 0xbf000900))
81
82 #define UART_LSR_FIFOE 0x80 /* Fifo error */
83 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
84 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
85 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
86 #define UART_LSR_FE 0x08 /* Frame error indicator */
87 #define UART_LSR_PE 0x04 /* Parity error indicator */
88 #define UART_LSR_OE 0x02 /* Overrun error indicator */
89 #define UART_LSR_DR 0x01 /* Receiver data ready */
90
91 int
mips_putc(char c,FILE * file)92 mips_putc(char c, FILE *file)
93 {
94 (void) file;
95 while ((uart.lsr & (UART_LSR_TEMT|UART_LSR_THRE)) != (UART_LSR_TEMT|UART_LSR_THRE))
96 ;
97 uart.data = (uint8_t) c;
98 return (unsigned char) c;
99 }
100
101 #ifdef TINY_STDIO
102
103 static int
mips_getc(FILE * file)104 mips_getc(FILE *file)
105 {
106 (void) file;
107 return EOF;
108 }
109
110 static FILE __stdio = FDEV_SETUP_STREAM(mips_putc, mips_getc, NULL, _FDEV_SETUP_RW);
111
112 #ifdef __strong_reference
113 #define STDIO_ALIAS(x) __strong_reference(stdin, x);
114 #else
115 #define STDIO_ALIAS(x) FILE *const x = &__stdio;
116 #endif
117
118 FILE *const stdin = &__stdio;
119 STDIO_ALIAS(stdout);
120 STDIO_ALIAS(stderr);
121
122 #endif
123