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